我想使用一个测试文件读取几个对象,但是我从步骤2到步骤4的空。
为姓名,如果我输入史蒂文,我想退回ste123(123应该是一个随机号码)
这是代码:
public class Lab14Tester
{
public static void main(String[] args)
{
Introduction step1 = new Introduction();
System.out.println(step1.giveline1());
System.out.println(step1.giveline2());
System.out.println(step1.giveline3());
System.out.println(step1.giveline2());
System.out.println(step1.giveline1());
Scanner in= new Scanner(System.in);
System.out.print("nName: ");
String inputname = in.nextLine();
Username step2 = new Username();
String givename = step2.givename();
System.out.println("Your username is " + givename);
System.out.print("nGive me a number: ");
int inputnumber =in.nextInt();
OddEven step3 = new OddEven();
System.out.println(step3.givenumbertype());
System.out.print("nGive me a number grade: ");
double grade = in.nextDouble();
NumberToLetter step4 = new NumberToLetter();
System.out.printf("n%.1f", grade);
System.out.print(" is equal to a " + step4.giveletter());
}
}
public class Username
{
private String subname;
private int randomnumber;
private String outputname;
public void getname(String inputname)
{
subname = inputname.substring(0,3);
randomnumber = (int)(Math.random()*1000);
outputname = subname + randomnumber;
}
public String givename()
{
return outputname;
}
}
您可能会遇到麻烦,因为您将字符串与整数串联。
尝试以下操作:
public void getname(String inputname)
{
subname = inputname.substring(0,3);
randomnumber = (int)(Math.random()*1000);
String randomnumberAsString = String.valueOf(randomnumber);
outputname = subname + randomnumberAsString;
}