Topic: 这个程序编译没问题,但运行没效果,请高手看看

  Print this page

1.这个程序编译没问题,但运行没效果,请高手看看 Copy to clipboard
Posted by: zhangp_happy
Posted on: 2005-08-05 10:26


import java.io.InputStream;
import java.io.IOException;

public class J_Echo
{
  public static void mb_echo(InputStream in)
  {
    try
    {
      while (System.in.toString() == "stop")
      {
        int i = in.read();
        if(i == -1)
        return;
        char c = (char) i;
        System.out.printCoffee;
      }
    }
    catch(IOException e)
    {
      System.err.printlnEnvelope;
    }
  }
  
  public static void main(String[] args)
  {
    mb_echo(System.in);
  }
}

编译 no problem
运行 看不到任何效果。
请大家告诉我为什么?

2.Re:这个程序编译没问题,但运行没效果,请高手看看 [Re: zhangp_happy] Copy to clipboard
Posted by: javalean
Posted on: 2005-08-05 20:20

你是想键盘输入一个字符串,然后输出它吧。
你的做法有很多不妥。当然具体如何我也解释不清楚,建议去看一些相关的instance(实例)
下面是我写的一个程序,用于输入一行字符串;按下回车后,在屏幕上输出它们。
//KeyinStr.java

import java.io.*;

public class KeyinStr{
public static void main(String [] args)throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s;

System.out.print("input a string:");
s=br.readLine();
System.out.print("result:");
System.out.println( s );
}
}

3.Re:这个程序编译没问题,但运行没效果,请高手看看 [Re: zhangp_happy] Copy to clipboard
Posted by: dabing
Posted on: 2005-08-05 20:43

while语句有问题

4.Re:这个程序编译没问题,但运行没效果,请高手看看 [Re: zhangp_happy] Copy to clipboard
Posted by: truthawp
Posted on: 2005-08-05 22:28

我不能确定楼主这个程序的目的,楼主是不是想写这样一个程序,程序目的是重复输入字符的内容,当输入"stop"时,程序退出?
我试了下把while中的==改成!=,可以实现重复输入字符,不过不知道怎样实现输入stop后退出,程序就这样无法退出了(我试着在while中加if判断然后break退出循环,不过好象没用,我也不清楚程序可重复操作是不是while语句的作用?)
2楼前辈的程序只能执行一次操作后自动退出~ 楼主大概要的不是这个效果吧~
Stupid

5.Re:这个程序编译没问题,但运行没效果,请高手看看 [Re: zhangp_happy] Copy to clipboard
Posted by: truthawp
Posted on: 2005-08-05 23:13

结合楼主和2楼前辈的程序,我基本上完成了,效果就是重复输入字符,stop退出

import java.io.*;
public class J_Echo
{
public static void mb_echo(InputStream in)
{
try
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String a=br.readLine();
String j="stop";
int o=1;
while (!(a.equals (j)))
{if(o==1)System.out.printlnAngel;
String b=br.readLine();
if(b.equals (j))break;
System.out.printlnBeer;
o++;
}
}
catch(IOException e)
{
System.err.printlnEnvelope;
}
}
public static void main(String[] args)
{
mb_echo(System.in);
}
}

楼主while循环体中的部分由于我是新手,不太明白,就自己改了 Smile

6.Re:这个程序编译没问题,但运行没效果,请高手看看 [Re: zhangp_happy] Copy to clipboard
Posted by: meteorping
Posted on: 2005-08-06 11:47

import java.io.*;
public class J_Echo
{
public static void mb_echo(InputStream in)
{
try
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String a=br.readLine();
String j="stop";
while (!(a.equals (j)))
{
System.out.println(a);
a=br.readLine();
}
}
catch(IOException e)
{
System.err.println(e);
}
}
public static void main(String[] args)
{
mb_echo(System.in);
}
}
楼上这样就可以了....有些不用的

7.Re:这个程序编译没问题,但运行没效果,请高手看看 [Re: zhangp_happy] Copy to clipboard
Posted by: truthawp
Posted on: 2005-08-06 11:57

恩 精简了循环体~ 受益了 Smile


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923