So verwandeln Sie Teile des Code in Methoden [geschlossen]Java

Java-Forum
Anonymous
 So verwandeln Sie Teile des Code in Methoden [geschlossen]

Post by Anonymous »

Bearbeiten: Zu diesem Zeitpunkt denke ich, dass ich besser verstanden habe, wie Methoden funktionieren. Ich verstehe jedoch nicht, wie ich meine Methode dazu bringen kann, eine ArrayList zurückzugeben. Hier ist die Methode, ich muss in der Lage sein, BMI.Get (i) später im Code zu verwenden, was mache ich falsch? < /P>

Code: Select all

static ArrayList calcBMI(ArrayList  feet, ArrayList  inch, ArrayList  wegt){
ArrayList BMI = new ArrayList();
for(int i = 0; i < feet.size(); i++) {
double x = ((wegt.get(i) / (Math.pow(((feet.get(i) * 12) + inch.get(i)), 2))) * 703);
BMI.add(x);
}
return ArrayList  BMI;
}
< /code>
Ich nehme eine Java -Klasse, in der wir auf einem Code aufgebaut werden, während wir lernen. Der Code gilt für einen Body Mass Index (BMI) -Conator. Ich bin nicht die besten Codierung, ich habe wirklich Schwierigkeiten zu verstehen, wie Methoden funktionieren sollen und wie ich sie im Code verwenden soll. Die Art und Weise, wie es im Unterricht erklärt wird, macht keinen Sinn und ich kann meinen Lehrer nicht danach fragen, während ich während seiner Bürozeiten arbeite. Ich bitte niemanden, meine Hausaufgaben zu machen, ich kann niemanden Else -Code verwenden, was ich suche, ist Ratschläge und weitere Problemlösung, wie ich dies herausgefunden habe. < /P>
Ich soll mindestens 3 Methoden anwenden. Die erste Methode ist eine BMI -Berechnung (statische DoppelcalcBMI), die die Eingangsarray -Listen aufnehmen und mir den BMI jeder Person geben sollte. Die zweite sollte, basierend darauf, ob die Person männlich oder weiblich ist, einen Verkoppelung zurückgeben, der mir sagt, ob die Person vorbei ist, unter, normal oder wahrscheinlich nicht übergewichtig (statische Chargenklasse). Der dritte sollte alle Benutzereingaben aufnehmen und sie in relevante Arraylisten speichern.  Mein Lehrer fügte hinzu: "Sie können wahrscheinlich alle Ihre Arraylisten in den Main erstellen und sie dann in eine statische Void -Methode weitergeben".import java.util.Scanner;
import java.util.ArrayList;

class assignment4b {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
ArrayList names = new ArrayList ();
ArrayList feet = new ArrayList ();
ArrayList inch = new ArrayList ();
ArrayList wegt = new ArrayList ();
ArrayList fat = new ArrayList ();
ArrayList sex = new ArrayList ();
char morepeople = 'y';
int counter = 1;

while(morepeople == 'y') {
System.out.println("Input data for user " + counter + ":");
counter ++;
System.out.println("Name?");
names.add(s.nextLine());
System.out.println("Height:");
System.out.println("Feet?");
feet.add(Integer.parseInt(s.nextLine()));
System.out.println("Inches?");
inch.add(Integer.parseInt(s.nextLine()));
System.out.println("Weight in pounds?");
wegt.add(Double.parseDouble(s.nextLine()));
System.out.println("Body fat percentage?");
fat.add(Integer.parseInt(s.nextLine()));
System.out.print("Enter male or female for sex.");
sex.add(s.nextLine().charAt(0));
System.out.println("Continue adding people? Enter y or n: ");
morepeople = s.nextLine().charAt(0);
}

int undCount = 0;
int normCount = 0;
int probNotCount = 0;
int overCount = 0;

int mundCount = 0;
int mnormCount = 0;
int mprobNotCount = 0;
int moverCount = 0;

int fundCount = 0;
int fnormCount = 0;
int fprobNotCount = 0;
int foverCount = 0;

int peopleCount = names.size();
int mCount = 0;
int fCount = 0;

ArrayList  bmi = new ArrayList ();

for(int i = 0; i < names.size(); i++){
String n = names.get(i);
int f = feet.get(i);
int in = inch.get(i);
double w = wegt.get(i);
int bf = fat.get(i);
char sx = sex.get(i);
double b = (w / (Math.pow(((f * 12) + in), 2)) * 703);
bmi.add(b);
System.out.println("Hello " + n + " your BMI is " + b + ".");
if(b < 18.5){
System.out.println("Underweight.");
undCount ++;
if(sx == 'f'){
fundCount ++;
fCount ++;}
if(sx == 'm'){
mundCount ++;
mCount ++;}
}
else if(b < 25) {
System.out.println("Normal weight.");
normCount ++;
if(sx == 'f'){
fnormCount ++;
fCount ++;}
if(sx == 'm'){
mnormCount ++;
mCount ++;}
}
else if((sx == 'm' && bf > 24) || (sx == 'f' && bf > 31)){
System.out.println("Overweight.");
overCount ++;
if(sx == 'f'){
foverCount ++;
fCount ++;}
if(sx == 'm'){
moverCount ++;
mCount ++;}
}
else {
System.out.println("Probably not overweight.");
probNotCount ++;
if(sx == 'f'){
fprobNotCount ++;
fCount ++;}
if(sx == 'm'){
mprobNotCount ++;
mCount ++;}
}
}

System.out.println("The percentage of males who are underweight is " + (100.0 * mundCount / mCount));
System.out.println("The percentage of males who are normal weight is "  + (100.0 * mnormCount / mCount));
System.out.println("The percentage of males who are overweight is " + (100.0 * moverCount / mCount));
System.out.println("The percentage of males who are probably not overweight is " + (100.0 * mprobNotCount / mCount));

System.out.println("The percentage of females who are underweight is " + (100.0 * fundCount / fCount));
System.out.println("The percentage of females who are normal weight is " + (100.0 * fnormCount / fCount));
System.out.println("The percentage of females who are overweight is " + (100.0 * foverCount / fCount));
System.out.println("The percentage of females who are probably not overweight is " + (100.0 * fprobNotCount / fCount));

}
}
< /code>
Ich habe es geschafft, die ersten beiden Methoden für eine einzelne Eingabeversion des Codes vorzustellen. Die Art und Weise, wie ich die Methoden gemacht habe, war wahrscheinlich nicht der beste Weg, aber ich verstehe nicht, wie es besser geht, und ich verstehe wirklich nicht, wie ich Methoden mit Array -Listen verwenden soll. < /P>
import java.util.Scanner;

class assignment3a {
static double calcBMI(double feet, double inch, double wegt){
double x = ((wegt / (Math.pow(((feet * 12) + inch), 2))) * 703);
return x;
}

static char weightClass(double x, String sex, double pfat){
if (x < 18.5) {
return 'u';
}
else if (x >= 18.5 && x < 25) { //n
return 'n';
}
else if (sex.equalsIgnoreCase("male") && pfat > 24 && x > 25) {
return 'o';
}
else if (sex.equalsIgnoreCase("female") && pfat > 31 && x > 25) {
return 'o';
}
else if (sex.equalsIgnoreCase("male") && pfat  25) {
return 'p';
}
else if (sex.equalsIgnoreCase("female") && pfat  25) {
return 'p';
}
else {
return 5;
}
}

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter a name: ");
String name = s.nextLine();
System.out.println("Please enter height, feet then inches");
System.out.println("Feet: ");
double feet = Double.parseDouble(s.nextLine());
System.out.println("Inches: ");
double inch = Double.parseDouble(s.nextLine());
System.out.println("Weight in pounds: ");
double wegt = Double.parseDouble(s.nextLine());
System.out.println("Body fat percentage: ");
double pfat = Double.parseDouble(s.nextLine());
System.out.println("Male or Female: ");
String sex = s.nextLine();

double x = calcBMI(feet, inch, wegt);

weightClass(x, sex, pfat);

System.out.println(name + "'s BMI is: " + x);

if (weightClass(x, sex, pfat) == 'u') {
System.out.println(name + " is underweight.");
}
else if (weightClass(x, sex, pfat) == 'n') {
System.out.println(name + " is normal weight.");
}
else if (weightClass(x, sex, pfat) == 'o') {
System.out.println(name + " is overweight.");
}
else if (weightClass(x, sex, pfat) == 'o') {
System.out.println(name + " is overweight.");
}
else if (weightClass(x, sex, pfat) == 'p') {
System.out.println(name + " is probably not overweight.");
}
else {
System.out.println(name + " is probably not overweight.");
}

}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post