Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.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.util.Scanner; /** * *@作者多诺万 */ 公共类控制器{ 公共控制员(){ 句子数组(); } 私有void语句数组(){ int size=0; 字串=”; int i=0; 扫描仪输入=新扫描仪(System.in); System.out.println(“您想输入多少单词?”); size=input.nextInt(); 字符串[]句子=新字符串[大小]; System.out.println(“请输入一个单词”); word=input.next(); 而(i_Java - Fatal编程技术网

而循环提示的单词比中要求的多一个单词 /* *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具|模板 *然后在编辑器中打开模板。 */ 包装控制器; 导入java.util.Scanner; /** * *@作者多诺万 */ 公共类控制器{ 公共控制员(){ 句子数组(); } 私有void语句数组(){ int size=0; 字串=”; int i=0; 扫描仪输入=新扫描仪(System.in); System.out.println(“您想输入多少单词?”); size=input.nextInt(); 字符串[]句子=新字符串[大小]; System.out.println(“请输入一个单词”); word=input.next(); 而(i

而循环提示的单词比中要求的多一个单词 /* *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具|模板 *然后在编辑器中打开模板。 */ 包装控制器; 导入java.util.Scanner; /** * *@作者多诺万 */ 公共类控制器{ 公共控制员(){ 句子数组(); } 私有void语句数组(){ int size=0; 字串=”; int i=0; 扫描仪输入=新扫描仪(System.in); System.out.println(“您想输入多少单词?”); size=input.nextInt(); 字符串[]句子=新字符串[大小]; System.out.println(“请输入一个单词”); word=input.next(); 而(i,java,Java,问题是您在i>=size之后请求输入 具体来说,这些线 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package controller; import java.util

问题是您在
i>=size
之后请求输入

具体来说,这些线

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.Scanner;

/**
 *
 * @author Donovan
 */
public class Controller {

    public Controller() {

        sentenceArray();
}

    private void sentenceArray() {
        int size = 0;
        String word = "";
        int i = 0;


        Scanner input = new Scanner(System.in);
            System.out.println("How many words would you like to enter?");
                size = input.nextInt();
                String [] sentence = new String[size];
                System.out.println("please enter a word.");
                word = input.next();



        while( i < size){

            if( i < size){
                sentence[i] = word;
                i++;
                System.out.println("Please enter a word.");

                word = input.next();

           }//end if
        }//end while

        displayArray(sentence);
    } // end sentence array


    public static void displayArray(String []sentence){
                System.out.print( "Your sentence is: " );

        for (String x : sentence){                
            System.out.print( x  + "\t" );
        }

    }//end displayResults


}//end controller
这不是C,所以可以在方法调用中声明变量

i++;
System.out.println("Please enter a word.");

word = input.next();
公共void语句数组(){
扫描仪输入=新扫描仪(System.in);
System.out.println(“您想输入多少单词?”);
int size=input.nextInt();
字符串[]句子=新字符串[大小];
int i=0;
而(i
边做边循环
我将使用一个
do-while
循环来测试循环体之后的条件,您不需要一个与循环锥方向匹配的
if
,也不需要临时的
字符串。大概

public void sentenceArray() {

    Scanner input = new Scanner(System.in);
    System.out.println("How many words would you like to enter?");
    int size = input.nextInt();
    String[] sentence = new String[size];

    int i = 0;
    while (i < size) {
        System.out.println("please enter a word.");
        String word = input.next();
        sentence[i] = word;
        i++;
    }

    displayArray(sentence);
}

据我所知,您要求的是多余的“请输入一个单词”?如果是,请删除上方的这些行,同时:

public static void displayArray(String[] sentence) {
    System.out.print("Your sentence is: ");

    for (String word : sentence) {
        System.out.printf("%s\t", word);
    }
    System.out.println();
}
请在此处检查整个代码:

System.out.println("please enter a word.");
word = input.next();
公共类控制器{
公共控制员(){
句子数组();
}
私有void语句数组(){
int size=0;
int i=0;
扫描仪输入=新扫描仪(System.in);
System.out.println(“您想输入多少单词?”);
size=input.nextInt();
字符串[]句子=新字符串[大小];
而(i0){
System.out.print(“您的句子是:”);
对于(字符串x:句子){
系统输出打印(x+“\t”);
}
}
}//最终显示结果
公共静态void main(字符串[]args){
控制器=新控制器();
}
}//终端控制器

希望这有帮助。

也许你有问题?
while(i
有点多余……我的类必须为这个赋值使用while循环,我去掉了if语句,因为我意识到它不是needed@uzid-好的,更新。任何for循环都可以转换为while,方法是保持条件,在while之前初始化变量,并在循环的最末端调整循环变量操作。使用此代码,分配给
句子[i]
的第一个
单词是一个空字符串。这就是为什么这些行在
上方,而
在我同意的情况下,则首选do-while。
“请输入一个单词。”
应该移到
input.next()
@cricket\u 007 Oops.修复。谢谢!
System.out.println("please enter a word.");
word = input.next();
   public class Controller {

    public Controller() {
        sentenceArray();
    }

    private void sentenceArray() {
        int size = 0;
        int i = 0;

        Scanner input = new Scanner(System.in);
        System.out.println("How many words would you like to enter?");
        size = input.nextInt();
        String [] sentence = new String[size];

        while( i < size){
            System.out.println("Please enter a word.");
            sentence[i] = input.next();
            i++;
            //end if
        }//end while

        displayArray(sentence);
    } // end sentence array


    public static void displayArray(String []sentence){
        if(sentence.length > 0) {
            System.out.print( "Your sentence is: " );

            for (String x : sentence){                
                System.out.print( x  + "\t" );
            }
        }

    }//end displayResults

    public static void main(String[] args) {
        Controller controller = new Controller();
    }
}//end controller