我在Java方面遇到了一些麻烦(还在学习)。我正试图制作一个程序来计算学生的数量,然后是注册所需的平板电脑数量,但我没有得到任何进展。我试过使用布尔运算符,但我只是得到语法错误。
我真正想要解决的问题是:
如果有40名或更少的学生,那么只需要一台平板电脑。如果人数超过40,那么每增加30名学生就需要再使用一片平板电脑。因此,41-70名学生需要2片,70至100名学生需要3片,依此类推。
您的输出应该包括所需的平板电脑数量和班上的学生人数:
这不是我所期望的,我被困住了,任何帮助都将是非常感谢的!
import javax.swing.JOptionPane;
public class Students {
public static void main(String[] args) {
int numberOfStudents;
int tablets = 1;
numberOfStudents=Integer.parseInt(JOptionPane.showInputDialog("Enter the number of students in the class: "));
if (numberOfStudents <= 40){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + " tablet for the e-register");
else if (numberOfStudents => 41 || <= 70){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + 1 + " tablets for the e-register");
else if (numberOfStudents => 71 || <= 100){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + 2 + " tablets for the e-register");
else if (numberOfStudents => 101 || <= 120){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + 3 + " tablets for the e-register");
{ }
}
}发布于 2015-11-18 11:42:54
你这里最大的问题是你对这个问题处理得很差。是的,我可以帮助你用布尔运算符来解决你的问题,但是你要输入多少次接近相同的if语句来允许越来越多的学生呢?一旦你做了其中的20个,如果你想改变它打印出来“你需要X学生的Y平板电脑”,会发生什么情况?你必须翻阅所有你已经打印出来的信息并修改它的地方。或者,如果有30,000名学生,而您的代码只包含允许1,000人的if条件,会发生什么情况?
这是你的程序应该做的,如果你必须在墙上写一个标记的话,你会在现实生活中这样做:
首先,让我们看看规则:
如果有40名或更少的学生,那么只需要一台平板电脑。如果人数超过40,那么每增加30名学生就需要再使用一片平板电脑。因此,41-70名学生需要2片,70至100名学生需要3片,依此类推。
首先,这是模棱两可的。40或更少需要一片平板:好的。然后它说,超过40人,那么每增加30名学生需要更多的平板电脑,这是有意义的,除了下一个例子。报告说,41名学生需要2片药片。然而,41并不是40岁以上的“额外30”。上面有一个。规则似乎是“如果人数超过40,那么需要额外的平板电脑,每增加30名学生就需要更多的平板电脑”。最后,报告说,41-70名学生需要2片,70至100名学生需要3片。呃,70名学生需要多少片?2片还是3片?
我想我们必须假设,因为41片是我们需要2片的点,71是我们达到3片的点。
现在让我们看看所有的代码。这一行得到了你的学生人数:
numberOfStudents=Integer.parseInt(
JOptionPane.showInputDialog("Enter the number of students in the class: "));这很好。现在,与其把所有这些假设放在一起,不如用数学来让计算机来决定我们需要多少平板电脑呢?计算机很擅长数学。
我们知道我们总是需要一块平板,对吧?因为"40或更少“的学生需要一台平板电脑。这样我们就可以开始了:
int tablets = 1;现在我们可以做下一步:如果有超过40名学生,那么我们有一个额外的平板电脑,再加上每30名学生额外增加一块平板电脑,超过原来的40名。如下所示:
if(numberOfStudents > 40) {
tablets += 1 + (numberOfStudents - 41) / 30;
}现在我们知道我们需要多少平板电脑,我们可以告诉用户。你已经做过这一步了--但是现在你只需要做一次。
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + " tablet for the e-register");这样做的另一个好处是,现在您的tablet变量保存了实际需要的平板电脑的数量。所以,如果你要再次使用这个值,它已经准备好了。在您最初发布的代码中,tablets变量始终只包含1。它也可能是一个常数。
通常,硬编码数字40和30将被转换为在代码顶部定义的常量。这将使代码的未来读者更容易理解这些数字。
编辑:只是为了显示所有代码在一起的样子:
numberOfStudents=Integer.parseInt(
JOptionPane.showInputDialog("Enter the number of students in the class: "));
int tablets = 1;
if(numberOfStudents > 40) {
tablets += 1 + (numberOfStudents - 41) / 30;
}
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + " tablet for the e-register");发布于 2015-11-18 11:01:28
您必须同时考虑这两个条件,并再次键入var名称。另外,在所有其他if语句开始之前放置一个结束大括号(})。最后,你不需要一个{ }。这是正确的密码。
if (numberOfStudents <= 40){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + " tablet for the e-register");
}
else if (numberOfStudents >= 41 && numberOfStudents <= 70){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + (tablets + 1) + " tablets for the e-register");
}
else if (numberOfStudents >= 71 && numberOfStudents <= 100){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + (tablets + 2) + " tablets for the e-register");
}
else if (numberOfStudents >= 101 && numberOfStudents <= 120){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + (tablets + 3) + " tablets for the e-register");
}发布于 2015-11-18 11:05:36
它缺少近距了吗?
if (numberOfStudents <= 40) {
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + " tablet for the e-register");
} else if (numberOfStudents >= 41 && numberOfStudents <= 70) {
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + 1 + " tablets for the e-register");
} else if (numberOfStudents >= 71 && numberOfStudents <= 100) {
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + 2 + " tablets for the e-register");
} else if (numberOfStudents >= 101 && numberOfStudents <= 120) {
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + 3 + " tablets for the e-register");
}更新:请注意:
<=而不是=<numberOfStudents >= 41 || numberOfStudents <= 70而不是numberOfStudents >= 41 || <= 70&&而不是||https://stackoverflow.com/questions/33778262
复制相似问题