导航:网站首页 >
java中用WHILE循环从键盘输入一位整数当输入1到7的时候对应的是,Java。使用while和dowhile循环解决。 从键盘
java中用WHILE循环从键盘输入一位整数当输入1到7的时候对应的是星...,Java。使用while和do-while循环解决。 从键盘上接收一批整数,比较并...
匿名网友:
package com.soft.Ipml;import java.util.Scanner;public class dfdsf {/*** @param args*/public static void main(String args) {int id=1;boolean flag=true;String week = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};//定义星期的字符数组Scanner input = new Scanner(System.in);//建立输入的一个对象while(flag==true){//tru /catch用来抛出你输入的非数字的异常,即如果你输入的是数字就运行try,否则运行catchtry {System.out.println"请输入:";id=input.nextInt);//从接受台接受数据if(id7){System.out.println"请重新输入!";//超出范围的数字,要重新输入}else{//这个if是对你列出的条件进行编译if(id>=0&&idif(id==0){flag = false;}else{System.out.println(weekid-1);}}}} catch (Exception e) {System.out.println"本回合结束,请重新启动程序!";flag = false;//非数字,回合结束}}}}
匿名网友:
import java.util.Scanner;public class test {public static void main(Stringargs){while(true){Scanner sc = new Scanner(System.in);int num=0;do{if (num>7||numSystem.out.println"输入有误!";break;}else{System.out.println"请输入0-7的数字:";num = sc.nextInt();switch(num){case 1: System.out.println("MON");break;case 2: System.out.println("TUE");break;case 3: System.out.println("WED");break;case 4: System.out.println("THU");break;case 5: System.out.println("FRI");break;case 6: System.out.println("SAT");break;case 7: System.out.println("SUN");break;}}}while(num!=0);if (num==0) {System.out.println"结束";break;}}}}
匿名网友:
#include char *txt = { "Mon.","Tue.","Wed.","Thu.","Fri.","Sat.","Sun." };int main(){int w;scanf("%d", &w);while (w != 0){if (1 printf("%s\n", txtw - 1);elseprintf("Error!\n");scanf("%d", &w);}return 0;}
匿名网友:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));boolean flag = true;while (flag) {System.out.println"请输入数字";String temp = null;try {while ((temp = br.readLine()) != null && flag) {switch (temp) {case "0":flag=false;break;case "1":System.out.println("Monday");break;case "2":System.out.println("tuesday");break;case "3":System.out.println("wesday");break;case "4":System.out.println("thursday");break;case "5":System.out.println("friday");break;case "6":System.out.println("saturday");break;case "7":System.out.println("sunday");break;default:System.out.println"请输入有效数字";break;}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}英文星期可能拼写不对,改一下就行
匿名网友:
试试如下代码:#include using namespace std;int main(){int a,b;float c; //平均数应为浮点型,因为结果不一定是整数int n;coutfor(a=0,b=0;;a++,b+=n,c=1.0*b/a) //整数相除,结果还是整数,会丢失小数部分,要乘1.0{cin>>n;if(n==0)break; //判断相等要用==}coutcoutcoutreturn 0;}
匿名网友:
输入1到7之间的值,输出星期几,其它值则退出程序。
c 语言程序如下:#include int main(){char s816={" ","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday" };int i;while(1){printf("input one int value between 1 to 7, other value exit:");scanf("%d",&i);if (i7) return 0;printf("%s\n\n",si);}return 0;}...
匿名网友:
throws IOException 是抛出异常,new Bufferedreader时会产生IOException异常,用throws IOException可将这个异常抛给类QqApp的父类,它本身不会处理这个异常。
更常用的方法是用try-catch自己处理异常。
从键盘输入整数是I/O流的内容,有很多种实现方法,除了标准输入流System.in外,常用的还有Scanner等 import java.util.*;//Scanner位于util包public class HelloFriend{String name;int age;HelloFriend(){Scanner in = new Scanner(System.in);//输入字符System.out.println("What's your name?");name = in.nextLine();//输入整形数据System.out.println("How old are U?");age = in.nextInt();} void display(){System.out.println("Hello, "+name+". Next year U'll be "+(age+1));}}public class InputTest{public static void main(String args){HelloFriend aFriend = new HelloFriend();aFriend.display();}}看了这个你就明白了
问题推荐