나는 간단한 평균 계산기를 만들고있다. 기본적으로 사용자 마크와 해당 모듈의 비율을 받아 평균 비율을 표시합니다. 프로그램은 작동하지만 while 루프에서 발생하는 몇 가지 결함이 있습니다. while 루프는 사용자가 -1에 값을 입력하자 마자 종료되지만 몇 번 계속 실행 한 다음 while 루프를 종료합니다. 또한 먼저 사용자가 while 루프를 시작할 수 있도록 숫자를 입력하게 한 다음 'Enter Mark'텍스트가 나타나 사용자가 다시 기호를 입력하게합니다. 임 while 루프를 자동으로 시작하려고하지만 너무 잘 모릅니다.사용자가 누른 내용에 따라 루프를 종료하는 방법은 무엇입니까?
import java.util.ArrayList;
import java.util.Scanner;
public class percentage {
static ArrayList<Double> marks = new ArrayList<>();
static ArrayList<Double> percentage = new ArrayList<>();
static Scanner input = new Scanner(System.in);
static void addingToMarks(double currentmark) {
marks.add(currentmark);
}
public static void main(String[] args) {
System.out
.println("Type in the number of marks you got \n"
+ "in the module. And then type the percentage weight of it.\n"
);
double exitLoop = input.nextDouble();
while (exitLoop > -1) {
System.out.println("Type in your marks");
marks.add(input.nextDouble());
System.out
.println("Type in the weighted percentage of the module: ");
percentage.add(input.nextDouble());
exitLoop = input.nextDouble();
}
System.out.println(percentage);
System.out.println(marks);
System.out.println("Your average percent for the module is: "
+ gradeCalculate());
}
static double gradeCalculate() {
double totalaverageweight = 0;
for (int x = 0; x < marks.size(); x++) {
totalaverageweight += ((marks.get(x)/100) * percentage.get(x));
}
return totalaverageweight;
}
}
@SummerCode 흠의 끝에서 일어날 것부터가 ... 루프가이 경우에 작동 할 때 생각 하지만 맞지 않아요;) – Charlie
'if (input == "whatever") {break; }' –
@SamIam : 예제는'input'이'Scanner'의 인스턴스이므로 모호합니다. 문자열을'=='와 비교하지 마십시오. ;) – MPirious