新人,求助个简单的问题

AnthonyWang_ 2013-03-14 11:37:37
import java.io.*;
public class T1
{
public static void main(String[] args) throws IOException
{
short num;
InputStreamReader a=new InputStreamReader (System.in);
BufferedReader b=new BufferedReader (a);
System.out.pritln("请输入成绩: "+"分");
String c=b.readLine();
num=Float.parseFloat(c);
if (num>=90)
System.out.println("优秀");
if (num>=80)
System.out.println("一般");
if (num>=70)
System.out.println("良好");
if (num>=60)
System.out.println("合格");
else System.out.println("不合格");
}
}
上面一个小程序里,在CMD上运行后输入一个数据后判断成绩了,怎么样弄可以在输入第二个数据的时候不用再运行一次java t1来进行判断成绩?
...全文
492 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
AnthonyWang_ 2013-03-21
  • 打赏
  • 举报
回复
弄了几天终于可以弄懂循环的了,感谢啊
Leonhe2022 2013-03-17
  • 打赏
  • 举报
回复
请笑纳~~~



	/**
	 * 
	 * @param input
	 *            输入的成绩
	 */
	private static void showResult(String input) {
		Float num;
		num = Float.parseFloat(input);
		if (num >= 90)
			System.out.println("优秀");
		else if (num >= 80)
			System.out.println("一般");
		else if (num >= 70)
			System.out.println("良好");
		else if (num >= 60)
			System.out.println("合格");
		else
			System.out.println("不合格");
	}

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.println("请输入成绩:  分. [ Q = 退出 ]");
			try {
				String input = scanner.nextLine();
				if (input.trim().length() > 0) {
					if ("Q".equalsIgnoreCase(input)) {
						break;
					}
					// 打印结果
					showResult(input);
				}

			} catch (Exception e) {
				System.out.println("输入不正确 " + e.toString());
			}
		}
		scanner.close();
	}


yanxing2012 2013-03-17
  • 打赏
  • 举报
回复
引用 13 楼 yyw6637 的回复:
请输入成绩: 分 50 不合格 请输入成绩: 分 60 合格 请输入成绩: 分 70 良好 合格 请输入成绩: 分 80 一般 良好 合格 请输入成绩: 分 exit
估计他在命令行测试,没有注意包的问题。。。
guoguoyong123 2013-03-17
  • 打赏
  • 举报
回复
用do while 循环 或 y用switch case
Karl_jiang 2013-03-17
  • 打赏
  • 举报
回复
while循环的问题,while会执行 大括号里的语句;while(){ }之间不能加分号 ,否则执行空语句,陷入死循环 ... 死循环:
while(!b.equals("exit"));   //执行空语句   ;
{ int a = 3;}  //这句不会执行
正确如下 :
while(!b.equals("exit")) 
{ int a = 3;}
AnthonyWang_ 2013-03-16
  • 打赏
  • 举报
回复
//我敲了17楼的方法发现可以,就是我想要的方法,因为老师现在还没有教.想先学着,我学着17楼的方法敲了另外一个问题,但是总是出错.,模仿不行 //求行李问题,如果行李小于20kg不用钱,超过20kg就1.2元1kg import java.io.*; public class T2 { public static void inner() throws Exception { double w,fee; InputStreamReader a=new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); System.out.println("请输入行李重量"); w=Double.parseDouble(b.readLine()); while(!b.equals("exit")); { if(w>20) fee=1.2*(w-20); else fee=0; System.out.println(""+w+"Kg行李需要交纳: "+fee+"元"); } System.exit(0); } public static void main(String[] args) throws Exception { inner(); } } 这个结果和上面的结果一样,都是在命令指示符上敲了,输入数据后就不能动了
AnthonyWang_ 2013-03-16
  • 打赏
  • 举报
回复
import java.io.*; public class T2 { private static void inner() throws Exception { short num; InputStreamReader a=new InputStreamReader(System.in); BufferedReader b=new BufferedReader(a); System.out.println("请输入成绩:"+"分"); String c=b.readLine(); while(!c.equals("exit")); { num=Short.parseShort(c); if (num>=90) System.out.println("优秀"); else if (num>=80) System.out.println("一般"); else if (num>=70) System.out.println("良好"); else if (num>=60) System.out.println("合格"); else System.out.println("不合格"); c=b.readLine(); } System.exit(0); } public static void main(String[] args) throws Exception { inner(); } } 我对着来敲了一遍怎么什么都没的
飞天0407 2013-03-15
  • 打赏
  • 举报
回复
做个while循环呗!
yyw6637 2013-03-15
  • 打赏
  • 举报
回复
请输入成绩: 分 50 不合格 请输入成绩: 分 60 合格 请输入成绩: 分 70 良好 合格 请输入成绩: 分 80 一般 良好 合格 请输入成绩: 分 exit
yyw6637 2013-03-15
  • 打赏
  • 举报
回复
引用 5 楼 a13376682420 的回复:
三楼的运行出错了
我运行怎么没错??
Karl_jiang 2013-03-15
  • 打赏
  • 举报
回复
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class A {
    private static void inner() throws Exception {
        short num;
        InputStreamReader a = new InputStreamReader(System.in);
        BufferedReader b = new BufferedReader(a);
        System.out.println("请输入成绩: " + "分");
        String c = b.readLine();
   // exit 跳出循环否则,不断读取输入;跳出执行system.exit(0),结束。
		while(!c.equals("exit")) {
			num = Short.parseShort(c);
			if (num >= 90)
				System.out.println("优秀");
			else if (num >= 80)
				System.out.println("一般");
			else if (num >= 70)
				System.out.println("良好");
			else if (num >= 60)
				System.out.println("合格");
			else
				System.out.println("不合格");
			c = b.readLine();
		}
		System.exit(0);
    }
 
    public static void main(String[] args) throws Exception {
        inner();
    }
}
这个程序没有很强的容错性,输入长int或者其他字母无法处理
Tnz_ME 2013-03-15
  • 打赏
  • 举报
回复
do while
yyw6637 2013-03-14
  • 打赏
  • 举报
回复
package test2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class A {
	private static void inner() throws Exception {
		short num;
		InputStreamReader a = new InputStreamReader(System.in);
		BufferedReader b = new BufferedReader(a);
		System.out.println("请输入成绩: " + "分");
		String c = b.readLine();
		if (c.equals("exit"))
			System.exit(0);
		else{
		num = Short.parseShort(c);
		if (num >= 90)
			System.out.println("优秀");
		if (num >= 80)
			System.out.println("一般");
		if (num >= 70)
			System.out.println("良好");
		if (num >= 60)
			System.out.println("合格");
		else
			System.out.println("不合格");
		}
		inner();
	}

	public static void main(String[] args) throws Exception {
		inner();
	}
}
「已注销」 2013-03-14
  • 打赏
  • 举报
回复
if(){ } else if(){ } 当if()成立时,就不进入else if()了;当不成立时,进入else if()块。
「已注销」 2013-03-14
  • 打赏
  • 举报
回复
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Test1 { public static void main(String[] args) throws IOException { float num; InputStreamReader a = new InputStreamReader(System.in); BufferedReader b = new BufferedReader(a); System.out.println("请输入成绩: " + "分"); String c = b.readLine(); num = Float.parseFloat(c); if (num >= 90) System.out.println("优秀"); else if (num >= 80) System.out.println("一般"); else if (num >= 70) System.out.println("良好"); else if (num >= 60) System.out.println("合格"); else System.out.println("不合格"); } }
xb12369 2013-03-14
  • 打赏
  • 举报
回复
System.out.println("请输入你的成绩:");
		Scanner scan = new Scanner(System.in);
		int score = scan.nextInt();
		if(score >=90){
			System.out.println("Very good");
		}else{
			System.out.println("go ahead");
		}
		System.out.println("**********just  demo ********");
xb12369 2013-03-14
  • 打赏
  • 举报
回复
大哥,你这是控制台程序 这个
 InputStreamReader a=new InputStreamReader (System.in);
回车了就没了!!! 做个网页版的,不行吗?
赏金--猎人 2013-03-14
  • 打赏
  • 举报
回复
错了 最外面套个while(true){ if(!c.equals("end")) return; }
赏金--猎人 2013-03-14
  • 打赏
  • 举报
回复
引用 7 楼 feng1160457741 的回复:
引用 6 楼 szx_zsx 的回复:在 String c=b.readLine();后面加个循环 while(!c.equals("end")){ ... }我在测试论坛
???
feng1160457741 2013-03-14
  • 打赏
  • 举报
回复
引用 6 楼 szx_zsx 的回复:
在 String c=b.readLine();后面加个循环 while(!c.equals("end")){ ... }
我在测试论坛
加载更多回复(3)

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧