问问这个怎么写 看着是简单的 但我一直写不对 可能学Java的时候默认我们c学的很好前面的基础一节课就过完了

问问这个怎么写 看着是简单的 但我一直写不对 可能学Java的时候默认我们c学的很好前面的基础一节课就过完了

img

img


import java.util.InputMismatchException;
import java.util.Scanner;

public class CLI {
    private static Scanner input = new Scanner(System.in);

    private static String readLine(String arg) {
        System.out.print(arg);
        return input.nextLine();
    }

    private static Integer readPosInt(String arg) {
        Integer i;
        while (true) {
            try {
                System.out.print(arg);
                i = input.nextInt();
                if (i < 0) {
                    System.out.println("Positive integers only!");
                } else {
                    break;
                }
            } catch (InputMismatchException e) {
                System.out.println("You must type an integer!");
            } finally {
                input.nextLine();
            }
        }
        return i;
    }

    public static void main(String[] args) {
        String str1 = readLine("Type some text: ");
        System.out.println("text read is " + str1);
        Integer i = readPosInt("Type an integer: ");
        System.out.println("Integer read is " + i);
        String str2 = readLine("Type some text again: ");
        System.out.println("text read is " + str2);
    }
}

你要写啥呀?C和java不一样,就算你不会C,也能学java,学会了java,也不一定就会C,只是学会了一门语言,不同的语言,思考问题的方式都差不多

无非就是让你理解 Scanner 对象的readLine() 和readPosInt() 这两个方法的使用,开始不会就先照着抄写,写完了删了,再按自己的理解再写几遍,然后修改调试、修改调试。。直到调试正确为止。编程这种东西千万别心急。

代码及运行结果如下:

img


代码:


import java.util.InputMismatchException;
import java.util.Scanner;

public class CLI {
    private static Scanner input = new Scanner(System.in);
    
    private static String readLine(String s){
        System.out.print(s);
        String t = input.nextLine();
        //input.nextLine(); //吸收回车符
        return t;
    }
    private static int readPosInt(String s){
        
        int t = 0;
        boolean b = true;
        while(b){
            System.out.print(s);
            try{
                t = input.nextInt();
                if(t>=0)
                    System.out.println("Postive integers only!");
                else
                    b = false;
            }catch(InputMismatchException e){
                System.out.println("You must type an integer!");
            }
            input.nextLine(); //接收回车符
        }
        
        return t;
    }
    
    public static void main(String[] args){
        String str1 = readLine("Type some text: ");
        System.out.println("Text read is: "+ str1);
        int i = readPosInt("Type an integer: ");
        System.out.println("Integer read is: "+ i);
        String str2 = readLine("Type some text again:");
        System.out.println("Text read is: "+ str2);
        
    }

}

你就用楼上那位贴完整代码的,把他的t>=0改成t<=0就可以了。

这大兄弟觉得搞java的英文都很好吗

跟题目的输出一模一样,一个字符都不差,源码如下

public class Answer7709520 {

    public static void main(String[] args) {
        String str1 = readLine("Type some text: ");
        System.out.println("Text read is: " + str1);
        int i = readPosInt("Type an integer: ");
        System.out.println("Integer read is: " + i);
        String str2 = readLine("Type some text again: ");
        System.out.println("Text read is: " + str2);
    }

    public static String readLine(String str) {
        System.out.print(str);
        Scanner scanner = new Scanner(System.in);
        return scanner.nextLine();
    }

    public static int readPosInt(String str) {
        while(true) {
            try {
                System.out.print(str);
                Scanner scanner = new Scanner(System.in);
                int i = scanner.nextInt();
                if(i<0) {
                    System.out.println("Positive integers only!");
                    continue;
                }
                return i;
            } catch (Exception e) {
                System.out.println("You must type an integer!");
            }

        }
    }
}

运行结果如下

Type some text: aaaa bbbb
Text read is: aaaa bbbb
Type an integer: cccc
You must type an integer!
Type an integer: dddd eeee
You must type an integer!
Type an integer: -100
Positive integers only!
Type an integer: -200
Positive integers only!
Type an integer: 1234
Integer read is: 1234
Type some text again: ffff gggg
Text read is: ffff gggg

img

纯手工输入和校对,
如有帮助,请采纳,十分感谢!

就是一个判断输入字符的类型啊