Java:空时停止输入

在我的程序中,我正在读取数字和符号,直到用户给我们空行。编辑:基本上程序应该模拟从堆栈(10 个元素的数组)中添加/删除数字,并且有两个基本操作(添加:+ 并在下一行中添加一个数字并起飞:对于每个成功添加的数字程序应该打印:)是不可能的(超出数组范围)打印::(并且为了可能的删除打印数字;(如下)

*SAMPLE:*


INPUT:

+

1

+

2

+

3

+

4

+

5

+

6

+

7

+

8

+

9

+

0

+

1

-

-

-

-

-

-

-

-

-

-

-

OUTPUT:

:)

:)

:)

:)

:)

:)

:)

:)

:)

:)

:(

0

9

8

7

6

5

4

3

2

1

:(

import java.util.*;

public class Zadanie3 {


public static void main(String[] args) {

    // TODO Auto-generated method stub

     Scanner input = new Scanner(System.in);

     String znak;

     

     char helper;

     int stack[]=new int[10];

     

     int i =-1;

     List<String> outcome = new ArrayList<>();

     

    

    while (input.hasNext()){

    

        znak=input.nextLine();

        if(znak.isEmpty()){

            break;

        }

        

        if(znak.charAt(0)=='+' && i<9){

            znak=input.nextLine();

            if(znak.isEmpty()){

                break;

            }

        i++;

        stack[i]=Integer.parseInt(znak);

        outcome.add(":)");

        

        }else if(znak.charAt(0)=='-' && i>=0 && i<=9){

        outcome.add(String.valueOf(stack[i]));

        i--;

        }

        else{

            outcome.add(":(");

            

        }

        znak=input.nextLine();

        

        if(znak.isEmpty()){

            break;

        }

        

        if(znak.charAt(0)=='+' && i<9){

            znak=input.nextLine();

            if(znak.isEmpty()){

                break;

            }

        i++;

        stack[i]=Integer.parseInt(znak);

        outcome.add(":)");

        

        }else if(znak.charAt(0)=='-' && i>=0 && i<=9){

        outcome.add(String.valueOf(stack[i]));

        

        i--;

        

        }

        

        else{

            outcome.add(":(");

        }

        

        }

      

    

    for(String s: outcome) {

        System.out.println(s);

    }

    

    

    

    

    

    

    

    

    

}

}


输入空行后仍然没有停止 - 我试过在 while 和 if 中使用 input.isEmpty() 但它也不起作用。我给了一些空白空间。从 while 中删除 hasNext() 并用 isEmpty() 替换它,Equals() 给出了相同的结果。)


慕村9548890
浏览 293回答 2
2回答

蓝山帝景

使小的工作示例考虑Scanner input = new Scanner(System.in);&nbsp; &nbsp; while (input.hasNextLine()){&nbsp; &nbsp;// test for new input&nbsp; &nbsp; &nbsp; &nbsp;String znak=input.nextLine();&nbsp; // get input&nbsp; &nbsp; &nbsp; &nbsp;if(znak.isEmpty()){&nbsp; &nbsp; &nbsp; // see if empty&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;System.out.println(znak);&nbsp; &nbsp; }

慕慕森

从更改代码if(znak.equals(""))到if(znak.isEmpty())内循环的同时在多个地方,从循环,取消条件打破&& !input.equals("")在while循环中,我不认为这是有道理有这个条件。试试下面import java.util.*;public class Zadanie3 {public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String znak = input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(znak.isEmpty()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char helper;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int stack[]=new int[10];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int i =-1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List<String> outcome = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; while (input.hasNext() ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; znak=input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(znak.isEmpty()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(znak.charAt(0)=='+' && i<9){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; znak=input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(znak.isEmpty()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stack[i]=Integer.parseInt(znak);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outcome.add(":)");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else if(znak.charAt(0)=='-' && i>=0 && i<=9){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outcome.add(String.valueOf(stack[i]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outcome.add(":(");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; znak=input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(znak.isEmpty()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(znak.charAt(0)=='+' && i<9){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; znak=input.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(znak.isEmpty()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stack[i]=Integer.parseInt(znak);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outcome.add(":)");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else if(znak.charAt(0)=='-' && i>=0 && i<=9){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outcome.add(String.valueOf(stack[i]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outcome.add(":(");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(String s: outcome) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(s);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java