JAVA问题
编写一个程序,输入三个大于10小于99的两位数,输出三个数中的最大数。这三个两位数之间以空格隔开。如输入12 23 34
追问:这是两个方法么?如题是什么意思啊,我需要把题目写上去么?还是直接写如题两字
编写一个程序,输入三个大于10小于99的两位数,输出三个数中的最大数。这三个两位数之间以空格隔开。如输入12 23 34
追问:这是两个方法么?如题是什么意思啊,我需要把题目写上去么?还是直接写如题两字
答案://系统产生 10-99 之间的三个整数,并输出最大数
public static void main(String[] args) {
int num[]=new int[3];
int max=0;
for (int i = 0; i < 3; i++) {
Random r=new Random();
int random=r.nextInt(100);
if(random<10){
while(true){
random=r.nextInt(100);
if(random>10){
break;
}
}
}else{
num[i]=random;
}
}
for (int j = 0; j < num.length; j++) {
if(max<num[j]){
max=num[j];
}
System.out.println("产生的随机数为:"+num[j]);
}
System.out.println("最大数字为:"+max);
}
//如题
public static void main(String[] args) {
int num[]={10,80,18};
int max=0;
for (int j = 0; j < num.length; j++) {
if(max<num[j]){
max=num[j];
}
}
System.out.println("最大数字为:"+max);
}
其他:public static void main(String[] args) {
Random ran = new Random();
int ran1=0;
int ran2=0;
int ran3=0;
while(true){
ran1 = ran.nextInt(100);
ran2 = ran.nextInt(100);
ran3 = ran.nextInt(100);
if(ran1>10 && ran2>10&& ran3>10){
break;
}
}
System.out.println(ran1+" "+ran2+" "+ran3);
} import java.util.Scanner;
import java.util.StringTokenizer;
public class SelectMax {
public static void main(String[] args) {
String strTemp = "";
int intTemp1 = 0;
int intTemp2 = 0;
int intTemp3 = 0;
Scanner s = new Scanner(System.in);
System.out.println("请输入3个数字:");
strTemp = s.nextLine();
StringTokenizer st = new StringTokenizer(strTemp, " ");
if (st.hasMoreTokens()) {
intTemp1 = Integer.parseInt(st.nextToken());
if(!check(intTemp1)) {
return;
}
}
if (st.hasMoreTokens()) {
intTemp2 = Integer.parseInt(st.nextToken());
if(!check(intTemp2)) {
return;
}
}
if (st.hasMoreTokens()) {
intTemp2 = Integer.parseInt(st.nextToken());
if(!check(intTemp2)) {
return;
}
}
System.out.println("最大的数字:" + getMax(intTemp1, intTemp2, intTemp3));
}
public static boolean check(int checknum) {
boolean flg = true;
if (checknum < 10 || checknum > 100) {
System.out.println("请重新运行并输入10到100之间的数字!");
flg = false;
}
return flg;
}
public static int getMax(int intTemp1, int intTemp2, int intTemp3) {
int Max = 0;
if (intTemp1 > intTemp2) {
Max = intTemp1;
} else {
Max = intTemp2;
}
if (intTemp3 > Max) {
Max = intTemp3;
}
return Max;
}
}
//------------------正常测试------------------------
请输入3个数字:
12 23 34
最大的数字:34
//------------------bug测试1------------------------
请输入3个数字:
1 23 34
请重新运行并输入10到100之间的数字!
//------------------bug测试2------------------------
请输入3个数字:
101 23 34
请重新运行并输入10到100之间的数字! 就是这个了编写一个程序,输入三个大于10小于99的两位数,输出三个数中的最大数。这三个两位数之间以空格隔开。如输入12 23 34
import java.util.Scanner;
import java.util.StringTokenizer;
public class SelectMax {
public static void main(String[] args) {
String strTemp = "";
int intTemp1 = 0;
int intTemp2 = 0;
int intTemp3 = 0;
Scanner s = new Scanner(System.in);
System.out.println("请输入3个数字:");
strTemp = s.nextLine();
StringTokenizer st = new StringTokenizer(strTemp, " ");
if (st.hasMoreTokens()) {
intTemp1 = Integer.parseInt(st.nextToken());
if(!check(intTemp1)) {
return;
}
}
if (st.hasMoreTokens()) {
intTemp2 = Integer.parseInt(st.nextToken());
if(!check(intTemp2)) {
return;
}
}
if (st.hasMoreTokens()) {
intTemp2 = Integer.parseInt(st.nextToken());
if(!check(intTemp2)) {
return;
}
}
System.out.println("最大的数字:" + getMax(intTemp1, intTemp2, intTemp3));
}
public static boolean check(int checknum) {
boolean flg = true;
if (checknum < 10 || checknum > 100) {
System.out.println("请重新运行并输入10到100之间的数字!");
flg = false;
}
return flg;
}
public static int getMax(int intTemp1, int intTemp2, int intTemp3) {
int Max = 0;
if (intTemp1 > intTemp2) {
Max = intTemp1;
} else {
Max = intTemp2;
}
if (intTemp3 > Max) {
Max = intTemp3;
}
return Max;
}
}
//------------------正常测试------------------------
请输入3个数字:
12 23 34
最大的数字:34
//------------------bug测试1------------------------
请输入3个数字:
1 23 34
请重新运行并输入10到100之间的数字!
//------------------bug测试2------------------------
请输入3个数字:
101 23 34
请重新运行并输入10到100之间的数字! import java.util.Scanner;
public class Test{
public static void main(String args[]){
System.out.print("请输入3个10-99数字:");
Scanner sc = new Scanner(System.in); //接收输入数据
String str=sc.nextLine(); //以字符串读入一行数据
String nums[]=str.split("\u0020"); //空格来拆分字符串
int numbers[]=new int[3]; //数组接收3个输入的数字
for(int i=0;i<3;i++){
int temp=Integer.parseInt(nums[i]); //字符串转化数字
if(check(temp)){ //向数组上设置数字
numbers[i]=temp;
}
}
System.out.println("最大的数字为:"+getMax(numbers));
}
public static boolean check(int num){ //检验数字
if(num>10&&num<100){
return true;
}else{
System.out.println("请输入10-99的数字!");
return false;
}
}
public static int getMax(int num[]){ //求最大数字
int max=num[0];
for(int i=0;i<num.length;i++){
if(max<num[i]){
max=num[i];
}
}
return max;
}
}
上一个:在xp上,如何调试java
下一个:java要考试了。。