gpt4 book ai didi

java - 使用java中的方法时无法循环

转载 作者:太空宇宙 更新时间:2023-11-04 08:50:35 24 4
gpt4 key购买 nike

对不起这些家伙,但我今天真的很不幸。请帮忙,我不久前的问题是循环时遇到问题,现在根本无法循环。它不会让我输入字符串以使其循环。名称、年份和部分也输出为空。我也尝试过使用 try catch 但它似乎没有发现任何错误。这是整个程序。它刚刚终止,恰好在:

enter 1 to continue;

import java.util.*;

public class program
{
public static Scanner rew= new Scanner(System.in);
public static String name, yearandsec;
public static char sex_code;

public static int scode;
public static double seq;
public static int ccode;
public static int units;
public static double fee_per_unit;
public static double misc;
public static double gross;
public static String dec;


public static String decision;

public static void main(String[] args) {
String dec;
do {
System.out.println("Input info:");
String name = stringGetter("Name: ");
String yearandsec = stringGetter("Year and section: ");

sex_code=charGetter("Sex code: " + "\n" + "[M]" + "\n" + "[F]:");
scode=intGetter("Scholarship code: ");
ccode=intGetter("Course code: ");
units=intGetter("Units: ");

fee_per_unit=doubleGetter("Fee per unit: ");
misc=doubleGetter("Miscellaneous: ");
display();
switches(scode, units, fee_per_unit, misc);

dec = stringGetterz("Enter 1 to continue: ");
} while(dec.equals("1"));
}

public static String stringGetterz(String ny){
System.out.println(ny);
return rew.nextLine();
}


public static char charGetter(String cg){
String sc;
char cc;
System.out.print(cg);
sc=rew.nextLine();
cc=sc.charAt(0);
return cc;

}




public static void switches(int scholar, int u, double fpu, double mi){
double discnt=0;
switch(scholar){
case 1:
System.out.println("Scholarship: Full Scholar..");
discnt=1;
break;
case 2:
System.out.println("Scholarship: Half Scholar..");
discnt=.50;
break;
case 3:
System.out.println("Scholarship: Dean's Lister..");
discnt=.15;
break;
case 4:
System.out.println("Scholarship: None..");
discnt=0;
break;
}

double gf;
double disc;
disc =(u * fpu) *discnt;
gf=(u * fpu) + mi - disc;
System.out.println(gf);


}

public static void switchcase(char gender, int corz){
double discnt=0;
switch(gender){
case 'M':
case 'm':
System.out.println("Sex: Male");
break;
case 'F':
case 'f':
System.out.println("Sex: Female");
break;
}




switch(corz){
case 1:
System.out.println("Course: BSIT...");

break;
case 2:
System.out.println("Course: BSCS..");
break;
case 3:
System.out.println("Course: BSCOE..");
break;


}




}



public static void display(){
System.out.println("Name: " + name + "\n" + "Year and Section: " + yearandsec);
System.out.println("Units enrolled: " + units + "\n" + "Fee per unit: " + fee_per_unit + "\n" + "Miscellaneous: " + misc);
switchcase(sex_code, ccode);







}

public static double doubleGetter(String dg){
double fm;
System.out.println(dg);
fm=rew.nextDouble();
return fm;


}
public static int intGetter(String scu){
int iget;
System.out.println(scu);
iget=rew.nextInt();
return iget;

}

public static String stringGetter(String ny){
String sget;
System.out.println(ny);
sget=rew.nextLine();
return sget;

}

}

最佳答案

静态字段在您第一次初始化类的实例时初始化,因此在您创建 new program() 之前,这些字段将不存在。此外,这些字段不会存在于主函数中。将当前的 main 方法重命名为 run 之类的名称,然后使用以下内容作为 main 方法:

public static void main(String args[]){
program p = new program();
p.run();
}

编辑:显然我错了,代码可以运行。显然,既然你运行了它......

通过将 stringGetterz 中的 rew.nextLine() 更改为 rew.next() 可以解决您的问题。不知道为什么。

关于java - 使用java中的方法时无法循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3429941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com