我想编写一个程序,输入1到4年的年代码和输出年级。
注意:使用if ... else语句和年份代码是字符类型。 (如果使用Array更好,我不允许使用数组,因为这是在条件控制结构中。在这之后,如何使用switch .... case语句编写这个程序?
如何声明char并从用户那里获得输入?
import java.util.Scanner;
import java.io.*;
public class CharStatement {
public static void main(String[] a) {
char userInput = new char;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter year code: ");
System.out.println("");
if (char == 1) {
System.out.println("First Year");
System.out.println("Freshmen");
}
else if ( char == 2) {
System.out.println("Second Year");
System.out.println("Sophomore");
}
else if (char == 3) {
System.out.println("Third Year");
System.out.println("Junior");
}
else if (char == 4) {
System.out.println("Fourth Year");
System.out.println("Senior");
}
else {
System.out.println("Invalid");
}
}
在java中接受单个字符有点乱。有关这方面的更多信息,请查看this。
您可以使用int
执行相同的操作,如下所示,作为代码中的修改,
System.out.println("");
int choice = keyboard.nextInt() // accepts an integer from the user
if (choice == 1) { // check if the given input is equal to 1
System.out.println("First Year");
System.out.println("Freshmen");
}
此外,您不能将变量命名为Java中的char
,因为它是keyword。
所以,修改后的代码使用if-else:
import java.util.Scanner;
public class IfStmt
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter year code: ");
int year=keyboard.nextInt();
System.out.println("");
if (year == 1)
{
System.out.println("First Year");
System.out.println("Freshmen");
}
else if ( year == 2)
{
System.out.println("Second Year");
System.out.println("Sophomore");
}
else if (year == 3)
{
System.out.println("Third Year");
System.out.println("Junior");
}
else if (year == 4)
{
System.out.println("Fourth Year");
System.out.println("Senior");
}
else
{
System.out.println("Invalid");
}
}
}
使用switch case修改代码:
import java.util.Scanner;
public class SwitchStmt
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter year code: ");
int year=keyboard.nextInt();
System.out.println("");
switch(year)
{
case 1:
System.out.println("First Year");
System.out.println("Freshmen");
break;
case 2:
System.out.println("Second Year");
System.out.println("Sophomore");
break;
case 3:
System.out.println("Third Year");
System.out.println("Junior");
break;
case 4:
System.out.println("Fourth Year");
System.out.println("Senior");
break;
default:
System.out.println("Invalid");
}
}
}
用它,它的工作原理。 ^ _ ^看到差异
import java.util.Scanner;
public class GradedExercise34
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter year code: ");
char in=keyboard.next( ).charAt(0);
System.out.println("");
if (in == '1')
{
System.out.println("First Year");
System.out.println("Freshmen");
}
else if ( in == '2')
{
System.out.println("Second Year");
System.out.println("Sophomore");
}
else if (in == '3')
{
System.out.println("Third Year");
System.out.println("Junior");
}
else if (in == '4')
{
System.out.println("Fourth Year");
System.out.println("Senior");
}
else
{
System.out.println("Invalid");
}
}
}