黑马程序员技术交流社区

标题: java如何实现在控制台循环输入 [打印本页]

作者: 狼牙    时间: 2013-8-1 08:59
标题: java如何实现在控制台循环输入
告急
谁遇到过这样的问题:
       java如何实现在控制台循环输入, 为什么只能输入一次之后,就不可以再输入了,
就算是循环实例化 Scanner input = new Scanner(System.in),也不可以啊?
求教?多谢路过的大神们!

作者: 吴光新    时间: 2013-8-1 09:17
多看看毕老师的视频,键盘录入那一块就能解决你这个问题
用while循环读取键盘录入的一行数据,输入over或你知道的字符串就结束循环

  1. <P> import java.io.*;
  2. class TransStreamDemo{
  3. public static void main(String[] args) throws IOException{
  4.      BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
  5.        BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));
  6.      System.out.println("请输入任意字母并按回车键:");
  7.      String line =null;
  8.      while((line=bufr.readLine())!=null)    {
  9.          if("over".equals(line))            //判断输入over,就结束循环
  10.              break;
  11.          bufw.write(line.toUpperCase());  
  12.          bufw.newLine();                    //换行
  13.          bufw.flush();                      //刷新
  14.         }
  15.      bufw.close();                          //关闭
  16.     }
  17. }</P>
复制代码

作者: 狼牙    时间: 2013-8-1 10:06
Jiewin 发表于 2013-8-1 09:17
多看看毕老师的视频,键盘录入那一块就能解决你这个问题
用while循环读取键盘录入的一行数据,输入over或你 ...

多谢!谢谢你!大哥!那么用Scanner类又该如何实现呢?
作者: zdew    时间: 2013-8-1 10:11
本帖最后由 zdew 于 2013-8-1 10:14 编辑
  1. class Demo
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 //Scanner类好像不太常用,最好不用吧
  6.                 Scanner scan = new Scanner(System.in);
  7.                 while(true)
  8.                 {
  9.                         System.out.println(scan.next());
  10.                 }
  11.         }
  12. }
复制代码

作者: 狼牙    时间: 2013-8-1 10:36
zdew 发表于 2013-8-1 10:11

这样好想无法实现不停地输入吧?我以前写的代码跟这个差不多,就无法实现!只能输入一个!
作者: 吴光新    时间: 2013-8-1 19:47
狼牙 发表于 2013-8-1 10:06
多谢!谢谢你!大哥!那么用Scanner类又该如何实现呢?

我基础测试的其中一题就用到了这个类,
那个代码太多,我做个简单的给你看看是不是你想要的
  1. import java.util.Scanner;

  2. public class ScannerDemo {
  3.         public static void main(String[] args) {
  4.                 String line = null;
  5.                 Scanner sc = new Scanner(System.in);
  6.                 //只要还有下一个
  7.                 while (sc.hasNext()) {
  8.                         //获取扫描器的下一个完整标记
  9.                         line = sc.next();
  10.                         //判断结束标记
  11.                         if ("over".equals(line))
  12.                                 break;
  13.                         System.out.println(line);
  14.                 }
  15.                 sc.close();
  16.         }
  17. }
复制代码

作者: 狼牙    时间: 2013-8-2 08:04
我可能没有说明白,我把我的代码贴出来,让大家帮我看一下,问题到底是出在哪里啦?
这是个学生类:
  1. package com.itheima;
  2. import java.util.*;
  3. import java.io.*;

  4. /**
  5. * 学生类
  6. * @author
  7. *
  8. */
  9. public class Student
  10. {
  11.         String name;
  12.         double c1,c2,c3;//成绩
  13.         static double sum;
  14.         /**
  15.          * 设置学生对象的属性
  16.          */
  17.         public void SetStudent()
  18.         {
  19.                 Scanner keyboard=new Scanner(System.in);
  20.                 System.out.println("请依次输入姓名,语文成绩,数学成绩,英语成绩,中间以逗号隔开");
  21.                  String getstr=keyboard.next();
  22.                  String[] str=getstr.split(",");
  23.                  this.name=str[0];
  24.                  this.c1=Double.parseDouble(str[1]);
  25.                  this.c2=Double.parseDouble(str[2]);
  26.                  this.c3=Double.parseDouble(str[3]);
  27.                  sum=c1+c2+c3;
  28.                  System.out.println("总成绩为:"+sum);
  29.         }
  30.        
  31.         /**
  32.          * 总成绩从高到低排序
  33.          * @throws IOException
  34.          */
  35.         public static void Sort(Student[] stu)
  36.         {
  37.                 for(int i=1;i<stu.length;i++)
  38.                 {
  39.                         if(stu[i].sum<stu[i-1].sum)
  40.                         {
  41.                                 stu[i-1]=stu[i];
  42.                         }
  43.                 }
  44.         }
  45.        
  46.         /**
  47.          * 把处理的数据写入文件
  48.          * @param Student类型的数组
  49.          * @throws IOException
  50.          */
  51.         public static void FileOp(Student[] stu)  throws IOException
  52.         {
  53.                 FileWriter fw=new FileWriter("stu.txt",true);
  54.                 BufferedWriter bw=new BufferedWriter(fw);
  55.                 try
  56.                 {
  57.                         for(int i=0;i<stu.length;i++)
  58.                         {
  59.                                 bw.write("姓名:"+stu[i].name);
  60.                                 bw.newLine();
  61.                                 bw.write("语文成绩:"+Double.toString(stu[i].c1));
  62.                                 bw.newLine();
  63.                                 bw.write("数学成绩:"+Double.toString(stu[i].c2));
  64.                                 bw.newLine();
  65.                                 bw.write("英语成绩:"+Double.toString(stu[i].c3));
  66.                                 bw.newLine();
  67.                                 bw.flush();
  68.                         }
  69.                         System.out.println("数据信息写入成功!");
  70.                 }
  71.                 catch(IOException e)
  72.                 {
  73.                         System.out.println("文件写入出错:"+e.getMessage());
  74.                 }
  75.                 finally
  76.                 {
  77.                         if(bw!=null)
  78.                                 bw.close();
  79.                 }
  80.         }
  81. }
复制代码
这是个调用类:
  1. package com.itheima;

  2. import java.io.IOException;

  3. /**
  4. * 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
  5. * 输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序写入
  6. * 到一个名称"stu.txt"文件中。
  7. * 要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
  8. * @author
  9. *
  10. */
  11. public class Test3 {

  12.         /**
  13.          * @param args
  14.          * @throws IOException
  15.          */
  16.         public static void main(String[] args) throws IOException
  17.         {
  18.                 //当使用下边的代码进行实例化,并设置学生的信息时,可以实现,但是代码太长,我想使用循环来实现
  19.                 Student s=new Student();
  20.                 s.SetStudent();
  21.                 Student s1=new Student();
  22.                 s1.SetStudent();
  23.                 Student s2=new Student();
  24.                 s2.SetStudent();
  25.                 Student s3=new Student();
  26.                 s3.SetStudent();
  27.                 Student s4=new Student();
  28.                 s4.SetStudent();
  29.                 Student[] stu=new Student[]{s,s1,s2,s3,s4};
  30.                 Student[] student=new Student[]{};
  31.                 //使用循环实现输入n多个学生信息(就是这里出的问题,只能输入一个学生的信息)
  32.                 for(int i=0;i<student.length;i++)
  33.                 {
  34.                         student[i].SetStudent();
  35.                 }
  36.                 try
  37.                 {
  38.                         Student.Sort(stu);
  39.                         Student.FileOp(stu);
  40.                 }
  41.                 catch(IOException e)
  42.                 {
  43.                         System.out.println("文件写入错误:"+e.getMessage());
  44.                 }
  45.         }

  46. }
复制代码
我想使用循环来实现学生信息的录入,而不是进行一个一个的设置才可以!请大神们帮一下忙!
作者: hmYing    时间: 2013-8-3 17:06
public static void main(String[] args){
    Scanner customIn = new Scanner(System.in);//不用将这句放入循环,循环中的语句尽量精简,以增加代码的执行效率
    //定义字符串数组接收用户的输入
    String strCustomIn[] = new String[100];
    int i = 0;
    do{
        i++;
        strCustomIn[i] = customIn.next();
        if(strCustomIn[i] == "no"){
            break;
        }
    }while(i<100);
}
作者: hmYing    时间: 2013-8-3 17:06
public static void main(String[] args){
    Scanner customIn = new Scanner(System.in);//不用将这句放入循环,循环中的语句尽量精简,以增加代码的执行效率
    //定义字符串数组接收用户的输入
    String strCustomIn[] = new String[100];
    int i = 0;
    do{
        i++;
        strCustomIn[i] = customIn.next();
        if(strCustomIn[i] == "no"){
            break;
        }
    }while(i<100);
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2