Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从用户java读取一行文本_Java_Loops_Java.util.scanner - Fatal编程技术网

从用户java读取一行文本

从用户java读取一行文本,java,loops,java.util.scanner,Java,Loops,Java.util.scanner,编写一个从用户处读取单行文本的程序。如果生成的字符串少于10个字符,该程序应打印“太短”;否则,它应该打印字符串中的字符数 这是我到目前为止得到的 package exercise; import java.util.Scanner; public class ex6 { public static void main(String[] args){ ///creates a scanner object Scanner input = new

编写一个从用户处读取单行文本的程序。如果生成的字符串少于10个字符,该程序应打印“太短”;否则,它应该打印字符串中的字符数

这是我到目前为止得到的

package exercise;

import java.util.Scanner;

public class ex6 {

    public static void main(String[] args){


        ///creates a scanner object
        Scanner input = new Scanner(System.in);

        //prompt the user to enter a line of text 
        System.out.print("Enter a line of text: " );
        String text = input.nextLine();

        //counts characters prints too short if text is less than 10 

        int counter = 0;
        for( int i=0; i < text.length(); i++ ) {
            if( text.charAt(i) == '$' ) { 
                counter++;
            }
                else if ( text.length() < 10){ 
                    System.out.println("To short"); 
            } 


            System.out.print("String Length :" );
            System.out.println(text.length());

        }
    }
}
package演习;
导入java.util.Scanner;
公共类ex6{
公共静态void main(字符串[]args){
///创建扫描仪对象
扫描仪输入=新扫描仪(System.in);
//提示用户输入一行文本
System.out.print(“输入一行文本:”);
String text=input.nextLine();
//如果文本小于10,则计数字符打印太短
int计数器=0;
对于(int i=0;i
这段代码的问题是,如果我输入man,例如,它将打印三次太短。下面的输出示例

输入一行文本:man 缩短 字符串长度:3 缩短 字符串长度:3 缩短
字符串长度:3

这是因为您正在循环字符串的长度。因此,当输入一个包含4个字符的字符串时,您将在条件中循环4次,并打印4次

public static void main(String[] args){


        ///creates a scanner object
        Scanner input = new Scanner(System.in);

        //prompt the user to enter a line of text
        System.out.print("Enter a line of text: " );
        String text = input.nextLine();

        //counts characters prints too short if text is less than 10

        if (text.length() < 10) {
            System.out.println("Too short");
        } else {
            System.out.print("String Length :" );
            System.out.println(text.length());
        }
    }
publicstaticvoidmain(字符串[]args){
///创建扫描仪对象
扫描仪输入=新扫描仪(System.in);
//提示用户输入一行文本
System.out.print(“输入一行文本:”);
String text=input.nextLine();
//如果文本小于10,则计数字符打印太短
if(text.length()<10){
System.out.println(“太短”);
}否则{
系统输出打印(“字符串长度:”);
System.out.println(text.length());
}
}

在这种情况下,应该避免使用循环。您只需要检查一次。

这是因为您正在循环字符串的长度。因此,当输入一个包含4个字符的字符串时,您将在条件中循环4次,并打印4次

public static void main(String[] args){


        ///creates a scanner object
        Scanner input = new Scanner(System.in);

        //prompt the user to enter a line of text
        System.out.print("Enter a line of text: " );
        String text = input.nextLine();

        //counts characters prints too short if text is less than 10

        if (text.length() < 10) {
            System.out.println("Too short");
        } else {
            System.out.print("String Length :" );
            System.out.println(text.length());
        }
    }
publicstaticvoidmain(字符串[]args){
///创建扫描仪对象
扫描仪输入=新扫描仪(System.in);
//提示用户输入一行文本
System.out.print(“输入一行文本:”);
String text=input.nextLine();
//如果文本小于10,则计数字符打印太短
if(text.length()<10){
System.out.println(“太短”);
}否则{
系统输出打印(“字符串长度:”);
System.out.println(text.length());
}
}
在这种情况下,应该避免使用循环。你只想检查一次