import java.io.*;
import java.util.Scanner;
public class Lab6_5 {
//
static String studentName ="NO NAME";
static int right = 0;
static int number1=0;
static int number2=0;
static int answer=0;
static double averageRight= 0.0;
static int counter=0;
//The main function
public static void main (String [] args) {
inputName ();
while (counter >=5) {
getNumber();
getAnswer ();
checkAnswer ();
counter= counter+1;
}
result ();
displayInfo ();
}
//
public static String inputName (){
Scanner keyboard = new Scanner(System.in);
System.out.print ("Enter student name: ");
studentName = keyboard.next();
return studentName;
}
//
public static int getNumber (){
number1 =(int)((Math.random() * 500) + 1);
number2 =(int)((Math.random() * 500) + 1);
return number2;
}
//
public static int getAnswer (){
System.out.println ("What is the answer to the following equation: ");
System.out.print(+number1);
System.out.print (" + ");
System.out.println (+number2);
Scanner keyboard = new Scanner (System.in);
System.out.println ("What is the sum: ");
answer = keyboard.nextInt ();
return answer;
}
//
public static int checkAnswer (){
if (number1 + number2 == answer) {
System.out.println ("Right");
right = right + 1;
}
else
System.out.println ("Wrong");
return right;
}
//
public static double result (){
averageRight = right/5;
return averageRight;
}
public static void displayInfo (){
System.out.println ("Information for student: "+studentName);
System.out.println ("The number right: "+right);
System.out.println ("The average right is: " +averageRight);
}
}
这是我的问题。该程序应该生成5个随机数学方程并检查答案。完成后,它会显示结果。唯一的问题是它没有循环。超过6个小时后,我发现最好寻求帮助。
答案 0 :(得分:3)
循环的条件 - while (counter >=5)
- 始终为false,因为counter
初始化为0。
更改
while (counter >=5)
到
while (counter <=5)
答案 1 :(得分:0)
您的问题是while (counter >=5)
是false
。
但真正的问题是,你为什么要使用所有方法和字段的类静态?
这不是一种使用java的好方法。
答案 2 :(得分:0)
while(counter&lt; = 5)
时更改while(counter&gt; = 5)答案 3 :(得分:0)
同时(计数器<= 5)
您的条件不正确。请更正。