如果要求用户输入整数和字母,应使用什么数据类型?
What data type to use if user is asked to input integers and letters?
我是编程新手。我的老师给了我一个关于数基的特殊项目。
由于较高的基数是整数和字母的组合,我仍然很困惑是否应该使用 int
and/or char
.
import java.util.Scanner;
public class JavaProgram {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the base you want to convert and base to be converted: ");
int firstBase = in.nextInt();
int secondBase = in.nextInt();
switch(firstBase) {
case 2:
System.out.print("Enter value: ");
// I get confused here already
case 3:
case 4:
case 5:
case 8:
case 10:
case 12:
case 16:
case 20:
case 24:
case 26:
case 27:
case 30:
case 32:
case 36:
}
in.close();
}
}
如果要存储数字和字母,可以使用 char
,因为从技术上讲,字母 和 数字是字符。或者,您可以使用 String
,它可以存储从单个字符到完整段落的任何内容。
以下是一些类型的简要概述:
char
- 只有一个字母或字符
int
- 只有数字没有小数
double
- 带小数的数字
String
- 数字和字母
因此根据您的要求,您必须 select String
。
我是编程新手。我的老师给了我一个关于数基的特殊项目。
由于较高的基数是整数和字母的组合,我仍然很困惑是否应该使用 int
and/or char
.
import java.util.Scanner;
public class JavaProgram {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the base you want to convert and base to be converted: ");
int firstBase = in.nextInt();
int secondBase = in.nextInt();
switch(firstBase) {
case 2:
System.out.print("Enter value: ");
// I get confused here already
case 3:
case 4:
case 5:
case 8:
case 10:
case 12:
case 16:
case 20:
case 24:
case 26:
case 27:
case 30:
case 32:
case 36:
}
in.close();
}
}
如果要存储数字和字母,可以使用 char
,因为从技术上讲,字母 和 数字是字符。或者,您可以使用 String
,它可以存储从单个字符到完整段落的任何内容。
以下是一些类型的简要概述:
char
- 只有一个字母或字符int
- 只有数字没有小数double
- 带小数的数字String
- 数字和字母
因此根据您的要求,您必须 select String
。