使用抛出java.io.IOException并获取System.in.read();通过整数访问案例的步骤
我只需要输入部分的帮助。如果用户输入一个数字,我需要程序读取并输出一个与输入的数字相等的大小写 //该程序将显示一年中的月份使用抛出java.io.IOException并获取System.in.read();通过整数访问案例的步骤,java,throws,Java,Throws,我只需要输入部分的帮助。如果用户输入一个数字,我需要程序读取并输出一个与输入的数字相等的大小写 //该程序将显示一年中的月份 public class MonthsOfTheYear { public static void main(String[] args) throws java.io.IOException{ int month; System.out.println("Please enter a Month Nu
public class MonthsOfTheYear {
public static void main(String[] args)
throws java.io.IOException{
int month;
System.out.println("Please enter a Month Number: ");
month = (int) System.in.read(); //Get an integer
switch (month) {
case 1: System.out.println("January");
break;
case 2: System.out.println("February");
break;
case 3: System.out.println("March");
break;
case 4: System.out.println("April");
break;
case 5: System.out.println("May");
break;
case 6: System.out.println("June");
break;
case 7: System.out.println("July");
break;
case 8: System.out.println("August");
break;
case 9: System.out.println("September");
break;
case 10: System.out.println("October");
break;
case 11: System.out.println("November");
break;
case 12: System.out.println("December");
break;
default: System.out.println("Invalid Month");
break;
}
System.out.println();
}
}
尝试改用Console类:获取用户输入,然后使用
Integer.parseInt(String)
另外,请注意,下一版本的Java(8)将支持大小写块中的字符串。您可以使用它来读取System.in
Scanner input = new Scanner(System.in);
month = input.nextInt();
输出:
Please enter a Month Number:
1
January
看
编辑:
正如noahz指出的,有一个控制台类涵盖了相同的功能。要了解两者之间的区别,请尝试使用TextIO输入函数
这比扫描仪输入更好
您需要先编译fist,这样才能在同一文件夹中找到TextIO.class
请改用此输入功能:
month=TextIO.getChar()
如果你在这个方法上有问题,请告诉我
谢谢 如果我错了,请纠正我的错误,但我很确定它仍然可以与“system.in.read”一起使用。您需要注意的是,在要处理的输入周围没有单引号。如果您想在键入“1”时运行case one,那么您的case应该是case“1”,而不是case 1。这里的问题是Java将字符强制转换为整数,因此您会得到一个ASCII值。
class vehicle
{
int passengers;
int fuelcap;
int mpg;
}
import java.io.*;
class Vehicle_Demo
{
public static void main (String args[]) throws java.io.IOException
{
vehicle obj1 = new vehicle();
obj1.passengers=12;
obj1.fuelcap=9;
obj1.mpg=78;
System.out.println(obj1.passengers, obj1.fuelcap, obj1.mpg);
}
}
例如,当你输入5时,你有53个ASCII码。使用System.out.println()中的变量值检查它代码>
我试图解决将月份更改为char类型,然后将char值传递给switch控件的问题。但问题是,获取System.in.read()代码>方法,您只需获得一个字符。最后,您可以在1到9之间进行选择。
我怀疑解决方案是使用缓冲区读取器。在此之前,我将我的代码留给您:
public class MonthsOfTheYear {
public static void main(String[] args)
throws java.io.IOException{
char month;
System.out.println("Please enter a Month Number: ");
month = (char) System.in.read(); //Get an integer
//System.out.println("Actual value of :" + month);
switch (month) {
case '1': System.out.println("January");
break;
case '2': System.out.println("February");
break;
case '3': System.out.println("March");
break;
case '4': System.out.println("April");
break;
case '5': System.out.println("May");
break;
case '6': System.out.println("June");
break;
case '7': System.out.println("July");
break;
case '8': System.out.println("August");
break;
case '9': System.out.println("September");
break;
/*case '10': System.out.println("October");
break;
case '11': System.out.println("November");
break;
case '12': System.out.println("December");
break;*/
default: System.out.println("Invalid Month");
break;
}
System.out.println();
}
}
真正的问题是什么?我不明白您的具体要求是什么。这可能就是您所寻找的控制台,它是在Java 6中引入的,专门用于更轻松地从命令行读取输入。见:。。。通过单击复选框图像。