这是我的代码:
package Class;
import java.util.Scanner;
public class Bicycle
{
public static int units;
public static int courseNum;
public static String courseName;
public Bicycle(int startUnits, int startNum, String startName)
{
units = startUnits;
courseNum = startNum;
courseName = startName;
}
public static int setUnits(int newValue)
{
units = newValue;
return units;
}
public static int setNum(int newValue)
{
courseNum = newValue;
return courseNum;
}
public static String setName(String newValue)
{
courseName = newValue;
return courseName;
}
public class subClass extends Bicycle
{
public int randVariable;
public subClass(int startUnits, int startNum, String startName)
{
super(startUnits, startNum, startName);
}
public void randVariable(int newValue)
{
randVariable = newValue;
}
}
public static void main(String args[])
{
int BaseUnits;
int BaseCourseNum;
String BaseCourseName;
int FinalUnits;
int FinalCourseNum;
String FinalCourseName;
Scanner entries = new Scanner(System.in);
BaseUnits = entries.nextInt();
BaseCourseNum = entries.nextInt();
BaseCourseName = entries.nextLine();
FinalUnits = setUnits(BaseUnits);
FinalCourseNum = setNum(BaseCourseNum);
FinalCourseName = setName(BaseCourseName);
System.out.printf("The Course ");
System.out.println(FinalCourseName);
System.out.printf(" (CIS %d) is worth %d units.", FinalCourseNum, FinalUnits);
}
}
我尝试做的是获取字符串输入,虽然我使用的代码会执行此操作,但程序实际上从未要求我输入该字符串。
答案 0 :(得分:0)
Scanner entries = new Scanner(System.in);
BaseUnits = entries.nextInt();
BaseCourseNum = entries.nextInt();
BaseCourseName = entries.next(); //<--
使用next()而不是nextLine(); 请阅读此nextLine以获得进一步说明。
答案 1 :(得分:0)
您的代码段正在接受来自用户的输入
扫描仪条目=新扫描仪(System.in);
BaseCourseName = entries.nextLine();
您可以将其更改为
BaseCourseName = entries.nextLine();
或
您也可以从BufferedReader获取输入,如下所示。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();