2 回答

TA贡献1818条经验 获得超7个赞
有几个问题很突出……
第一的...
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
您不需要多个扫描仪,无论如何它们都从同一个流中读取,最好只使用一个并降低复杂性。
下一个...
String scanner1 = sc1.nextLine();
name [i] = scanner1;
System.out.println("Input weight: ");
double scanner2 = sc2.nextDouble();
if(!sc1.hasNextDouble())
{
System.out.println("Invalid Weight!. Start Again");
} else
{
weightkg[i] = scanner2;
}
使用时nextDouble,缓冲区仍包含换行符,这意味着下次使用时nextLine,它将返回空白String并继续。
此外,hasNextDouble似乎在等待数据,但您已经double从缓冲区中读取了值,留下了悬空的新行。在我的测试中,这导致程序等待更多输入出现问题。
我通过做这样的事情“解决”了基本问题......
String scanner1 = sc1.nextLine();
name [i] = scanner1;
System.out.println("Input weight: ");
double scanner2 = sc1.nextDouble();
weightkg[i] = scanner2;
sc1.nextLine();
现在这个“将”起作用,但这不是最好的解决方案。“不同”的方法可能是将权重作为 a 读取String并尝试将其解析为 a double,这使您有机会捕获无效输入并以更合适的方式处理它,例如......
System.out.println("Input name: ");
String scanner1 = sc1.nextLine();
name[i] = scanner1;
boolean done = false;
double weight = 0;
do {
System.out.println("Input weight: ");
String input = sc1.nextLine();
try {
weight = Double.parseDouble(input);
done = true;
} catch (NumberFormatException nfe) {
System.out.println("!! Invalid value");
}
} while (!done);
weightkg[i] = weight;
System.out.println("Name: " + name[i]);
System.out.println("weight : " + weightkg[i]);
}

TA贡献1804条经验 获得超7个赞
你的代码中有一些逻辑错误。在每一行之后我都会提到它们。
import java.util.Scanner;
public class HowTo {
public static void main(String[] args) {
Scanner sc1 = new Scanner(System.in); // you need only 1 scanner
double weightkg[] = new double[30];
double weightkgEndOfMonth[] = new double[30];
String name[] = new String[30];
double weightDifference[] = new double[30];
for (int i = 0; i < 30; i++) { // need to iterate from 0 index to 29
System.out.print("Input name: ");
String scanner1 = sc1.nextLine();
name[i] = scanner1;
System.out.print("Input weight: ");
if (!sc1.hasNextDouble()) {
System.out.println("Invalid Weight!. Start Again");
} else {
weightkg[i] = sc1.nextDouble();// if it has double then read it
}
System.out.println("Name: " + name[i]);
System.out.println("weight : " + weightkg[i]);
sc1.nextLine();
}
for (int i = 0; i < 30; i++) {// need to iterate from 0 index to 29
System.out.println("Input weight at the end of month: ");
if (!sc1.hasNextDouble()) {
System.out.println("Invalid Weight!. Start Again");
} else {
weightkgEndOfMonth[i] = sc1.nextDouble();// read double here
}
weightDifference[i] =weightkgEndOfMonth[i]- weightkg[i] ;// weight difference is (final weight- initial weight)
if (weightDifference[i] > 2.5) {
System.out.println("Student with a weight difference greater than 2.5kg: " + name[i]);
System.out.println("Weight difference: " + weightDifference[i]);
System.out.println("Rise");
}
if (weightDifference[i] < -2.5) {// fall if weight less than 2.5
System.out.println("Student with a weight difference greater than 2.5kg: " + name[i]);
System.out.println("Weight difference: " + weightDifference[i]);
System.out.println("Fall");
}
}
}
}
现在它工作正常。
没有找到匹配的内容?试试慕课网站内搜索吧
添加回答
举报