package gradrequirment;
import java.util.Scanner;
public class graduation {
public static void main(String[] args) {
该程序显示您是否可以从高中毕业。.
Scanner gpa = new Scanner(System.in);
Scanner math_credit = new Scanner(System.in);
Scanner english_credit = new Scanner(System.in);
Scanner science_credit = new Scanner(System.in);
Scanner socialstudies_credit = new Scanner(System.in);
Scanner service_hours = new Scanner(System.in);
Scanner physed3 = new Scanner(System.in);
Scanner arts3 = new Scanner(System.in);
boolean physed;
boolean art;
boolean hope1;
boolean art1;
我尝试过的char输入可能起作用吗?
System.out.println("Enter your GPA, service hours, and credits for each class.");
System.out.println("");
System.out.println("Enter GPA:");
double GPA = gpa.nextDouble();
System.out.println("Enter Service Hours:");
int hours = service_hours.nextInt();
System.out.println("Enter Y/N if you have take HOPE.");
char hope = physed3.next().charAt(0);
System.out.println("Enter in How Many Math Credits You Have:");
int math = math_credit.nextInt();
System.out.println("Enter in English Credits:");
int english = english_credit.nextInt();
System.out.println("Enter in Science Credits:");
int science = science_credit.nextInt();
System.out.println("Enter in Social Studies Credits:");
int studies = socialstudies_credit.nextInt();
System.out.println("Enter Y/N if You Have A Fine Art Credit.");
char arts = arts3.next().charAt(0);
所有的布尔值和if-elses似乎都能正常运行。
if (math >= 4)
{
boolean math1 = true;
}
else {
boolean math1 = false;
if (english >= 4)
{
boolean english1 = true;
}
else {
boolean english1 = false;
if (science >= 3)
{
boolean science1 = true;
}
else {
boolean science1 = false;
if (studies >= 3)
{
boolean studies1 = true;
}
else {
boolean studies1 = false;
if (hours >= 100)
{
boolean hours1 = true;
}
else {
boolean hours1 = false;
if (GPA >= 2.0)
{
boolean gpa1 = true;
}
else {
boolean gpa1 = false;
if (physed3.next().charAt(0) == (89))
physed = true;
else {
physed = false;
if (arts3.next().charAt(0) == (89))
art = true;
else {
art = false;
if (physed == true)
{
hope1 = true;
}
else {
hope1 = false;
}
if (art == true)
{
art1 = true;
}
else {
art1 = false;
最后一个if else语句没有可见错误,但没有输出。
if(art1 && hope1 && gpa1 && hours1 && gpa1 && studies1 && science1 && english1 && math1 == true)
System.out.println("You Have Graduated!");
else
System.out.println("You Have Not Graduated. Try Harder!");
gpa.close();
math_credit.close();
science_credit.close();
socialstudies_credit.close();
english_credit.close();
service_hours.close();
{
}
}
}
}
}
}
}
}
}
}
}
}
我对Java还是比较陌生的,所以我不知道是否有明显的错误。
答案 0 :(得分:2)
您面临的主要问题在于if else构造。如果math(您从扫描仪输入的值)的值> = 4,则永远不会得到输出。因为您将输出打印在else块中。同样,即使您的数学<= 4,在if (physed3.next().charAt(0) == (89))
的else块中打印输出时,您的语句也不会执行。因此,直到if {block}直到if (art == true)
的所有条件都失败,您才能获得输出。因此,看来您的if else构造是错误的。因此,下面的代码块根本没有执行。
if(art1 && hope1 && gpa1 && hours1 && gpa1
&& studies1 && science1 && english1 && math1 == true)
System.out.println("You Have Graduated!");
else
System.out.println("You Have Not Graduated. Try Harder!");
答案 1 :(得分:0)
您的状况
if(art1 && hope1 && gpa1 && hours1 && gpa1 && studies1 && science1 && english1 && math1 == true)
始终为false
。这就是为什么您无法获得输出的原因
答案 2 :(得分:0)
我修改了程序并使它变得简单。现在它显示出正确的结果。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean physed,art,hope1,art1,gpa1,hours1,studies1,science1,english1,math1;
System.out.println("Enter your GPA, service hours, and credits for each class.");
System.out.println("");
System.out.println("Enter GPA:");
double GPA = input.nextDouble();
System.out.println("Enter Service Hours:");
int hours = input.nextInt();
System.out.println("Enter Y/N if you have take HOPE.");
char hope = input.next().charAt(0);
System.out.println("Enter Y/N if You Have A Fine Art Credit.");
char arts = input.next().charAt(0);
System.out.println("Enter in How Many Math Credits You Have:");
int math = input.nextInt();
System.out.println("Enter in English Credits:");
int english = input.nextInt();
System.out.println("Enter in Science Credits:");
int science = input.nextInt();
System.out.println("Enter in Social Studies Credits:");
int studies = input.nextInt();
if (math >= 4)
math1 = true;
else
math1 = false;
if (english >= 4)
english1 = true;
else
english1 = false;
if (science >= 3)
science1 = true;
else
science1 = false;
if (studies >= 3)
studies1 = true;
else
studies1 = false;
if (hours >= 100)
hours1 = true;
else
hours1 = false;
if (GPA >= 2.0)
gpa1 = true;
else
gpa1 = false;
if (hope == 'Y')
physed = true;
else
physed = false;
if (arts == 'Y')
art = true;
else
art = false;
if (physed == true)
hope1 = true;
else
hope1 = false;
if (art == true)
art1 = true;
else
art1 = false;
if(art1 && hope1 && gpa1 && hours1 && gpa1 && studies1 && science1 && english1 && math1 == true)
System.out.println("You Have Graduated!");
else
System.out.println("You Have Not Graduated. Try Harder!");
}
我已将变量类型更改为boolean,然后从项目中删除了多余的语句。