Hallo Leute,
habe mal wieder eine Frage.
Im Moment habe ich große Probleme Struktur in meinen Programmcode zu bringen.
Ich lerne nach einem Buch, das heißt: "Building Java Programs - A Back to Basic Approach" aber leider bin ich anscheinend nicht gescheit genug zu verstehen wie das genau funktionieren soll, denn ich bekomme es einfach nicht hin, trotz guter Beispiele.
Bin nun im 4ten Kapitel und soll folgende Aufgabe bearbeiten:
Ich habe überhaupt kein Problem die Aufgabe zu lösen, mein Code sieht dann so aus:
Ihr seht sicher schon wo das Problem ist...
Laut meinem Buch ist das eine schlechte Lösung alles in die Main-Methode zu packen weil es das Programm unübersichtlich werden lässt und man solle es doch in verschiedene Untermethoden teilen.
Damit habe ich aber nun größte Schwierigkeit
Ich bin hier echt schon am Verzweifeln, eigentlich wollte ich auch noch die Rechnung und das Eingeben des Integers trennen aber dann war das nur NOCH verwirrender.
Sitze an dem Problem nun schon eine ganze Weile und komme einfach nicht weiter, in dem Beispiel was angeführt wurde fande ich das schon ziemlich schwer nachzuvollziehen aber da ging das noch einigermaßen aber hier kann ich das einfach nicht mehr anwenden.
Kann mir vielleicht bitte einer helfen?
PS: Das heir ist das Beispiel, nach dem ich meine Programme schreiben möchte, da es sehr übersichtlich aussieht.
habe mal wieder eine Frage.
Im Moment habe ich große Probleme Struktur in meinen Programmcode zu bringen.
Ich lerne nach einem Buch, das heißt: "Building Java Programs - A Back to Basic Approach" aber leider bin ich anscheinend nicht gescheit genug zu verstehen wie das genau funktionieren soll, denn ich bekomme es einfach nicht hin, trotz guter Beispiele.
Bin nun im 4ten Kapitel und soll folgende Aufgabe bearbeiten:
Write a method called fractionSum that accepts an integer parameter n and returns as a double the sum of the first n terms of the sequence
In other words, the method should generate the following sequence:
1 + 1/2 + 1/3 + 1/4 ... etc. also 1/n + 1/n+1 etc
Ich habe überhaupt kein Problem die Aufgabe zu lösen, mein Code sieht dann so aus:
Java:
import java.util.*;
public class Chapter_4_Exercise_1_BilligLösung {
public static void main(String[] args){
Scanner console = new Scanner(System.in);
//Introduction to the program
System.out.println("This programm accepts an integer parameter\n" +
"n and returns it as a double of the sum of the first" +
"n terms of the sequence");
System.out.println();
//Prompts the user for input
System.out.print("Please enter your parameter as an integer: ");
int input = console.nextInt();
//Calculates
double sum = 0;
for (double i = 1; i <= input; i++){
sum += 1/i;
}
//Returns the result
System.out.println("Sie haben die Zahl: " + input + " eingeben\n" +
"Die Summe ist: " + sum);
}
}
Ihr seht sicher schon wo das Problem ist...
Laut meinem Buch ist das eine schlechte Lösung alles in die Main-Methode zu packen weil es das Programm unübersichtlich werden lässt und man solle es doch in verschiedene Untermethoden teilen.
Damit habe ich aber nun größte Schwierigkeit
Java:
import java.util.*;
public class Chapter_4_Exercise_1 {
public static void main(String[] args){
Scanner console = new Scanner(System.in);
introduction();
int input = getResult(console);
result(input, sum);
}
//Introduction to the program
public static void introduction(){
System.out.println("This programm accepts an integer parameter\n" +
"n and returns it as a double of the sum of the first" +
"n terms of the sequence");
System.out.println();
}
//Prompts the user for input
public static int getResult(Scanner console){
System.out.print("Please enter your parameter as an integer: ");
int input = console.nextInt();
for (double i = 1; i <= input; i++){
double sum = 0;
sum += 1/i;
}
return input;
}
//Returns the result
public static void result(int input, double sum){
System.out.print("You entered the numer : " + input +
"The sum is: " + sum);
}
}
Ich bin hier echt schon am Verzweifeln, eigentlich wollte ich auch noch die Rechnung und das Eingeben des Integers trennen aber dann war das nur NOCH verwirrender.
Sitze an dem Problem nun schon eine ganze Weile und komme einfach nicht weiter, in dem Beispiel was angeführt wurde fande ich das schon ziemlich schwer nachzuvollziehen aber da ging das noch einigermaßen aber hier kann ich das einfach nicht mehr anwenden.
Kann mir vielleicht bitte einer helfen?
PS: Das heir ist das Beispiel, nach dem ich meine Programme schreiben möchte, da es sehr übersichtlich aussieht.
Java:
// This program finds the body mass index (BMI) for two
// individuals. This variation includes several methods
// other than main.
import java.util.Scanner;
public class bmi_1_own_b {
public static void main(String[] args){
Scanner console = new Scanner(System.in);
introduction();
double bmi1 = getBMI(console);
double bmi2 = getBMI(console);
reportResults(bmi1, bmi2);
}
// introduces the program to the user
public static void introduction(){
System.out.println("This program reads data for two\n" +
"people and computes their body\n" +
"mass index and weight status.\n");
}
// prompts for one person’s statistics, returning the BMI
public static double getBMI(Scanner console){
System.out.println("Enter next person's information: ");
System.out.print("height in m? ");
double height = console.nextDouble();
System.out.print("weight in kg? ");
double weight = console.nextDouble();
double bmi = BMIFor(height, weight);
return bmi;
}
// reports the weight status for the given BMI value
public static void reportStatus(double bmi){
if (bmi < 18.5){
System.out.println("underweight");
} else if (bmi < 24.9) {
System.out.println("normal");
} else if (bmi < 29.9) {
System.out.println("overweight");
} else {
System.out.println("obese");
}
}
// this method contains the body mass index formula for
// converting the given height (in meter) and weight
// (in kilogram) into a BMI
public static double BMIFor(double height, double weight) {
return weight / (height * height);
}
// reports the overall bmi values and weight status
public static void reportResults(double bmi1, double bmi2){
System.out.printf("Person #1 body mass indes = %5.2f\n", bmi1);
reportStatus(bmi1);
System.out.printf("Person #1 body mass indes = %5.2f\n", bmi2);
reportStatus(bmi2);
}
}