Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.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_While Loop - Fatal编程技术网

Java-在一行中输出整数到用户输入整数

Java-在一行中输出整数到用户输入整数,java,while-loop,Java,While Loop,我的任务是创建一个程序,提示用户输入一个整数,然后使用while循环基于用户输入创建一个乘法表 输出乘法表必须包括标题行和列。我一直关注的是如何创建标题行,因为标题必须计数的数字取决于用户输入 我在while循环中创建了header列来格式化行,因为每次循环通过时都会创建一个新行。但是标题行只打印一次,因此我不能使用格式化列的while循环来创建它 如何创建标题行,以便将用户输入减回到1,并将所有整数值从1、2、3、,用户输入是输出吗 以下是我所拥有的: import java.util.Sca

我的任务是创建一个程序,提示用户输入一个整数,然后使用while循环基于用户输入创建一个乘法表

输出乘法表必须包括标题行和列。我一直关注的是如何创建标题行,因为标题必须计数的数字取决于用户输入

我在while循环中创建了header列来格式化行,因为每次循环通过时都会创建一个新行。但是标题行只打印一次,因此我不能使用格式化列的while循环来创建它

如何创建标题行,以便将用户输入减回到1,并将所有整数值从1、2、3、,用户输入是输出吗

以下是我所拥有的:

import java.util.Scanner;
public class Multiplication
{
    public static void main(String[] args)
    {
    int number;
    String yesno = "Y";
    Scanner keyboard = new Scanner(System.in);
    System.out.print ("This program generates a multiplication table ");
    System.out.println("based on a number you enter.");


    while(yesno.equalsIgnoreCase("Y"))
    {
        System.out.println("Please enter a number.");
        number = keyboard.nextInt();

        int row = 1;
        int column = 1; 

        System.out.println(" |" + MAKETHISWORK + "\t"); //THIS IS WHERE I'M STUCK.
        System.out.println("--------------------");          

        while(number>0)  
        {     
            row = 1;
            while(row <= number)
            {
                System.out.print(row + "|");

                column = 1; 
                while(column <= number)
                {

                    System.out.print(row*column + "\t");   
                    column ++;

                } //END WHILE COLUMN <= NUMBER
                System.out.println();
                row ++;

            } //END WHILE ROW <=NUMBER
            System.out.println();

            while(number>0)
            {
                number--;
            }

        } //END WHILE NUMBER >0

        System.out.println("Would you like to enter another number? (Y/N) ");
        yesno = keyboard.nextLine();
        yesno = keyboard.nextLine();
    } //END WHILE YESNO IS Y

    while(yesno.equalsIgnoreCase("N"))
    {
        System.out.println("Thanks for participating!");
        break;
    }
}
}
import java.util.Scanner;
公共类乘法
{
公共静态void main(字符串[]args)
{
整数;
字符串yesno=“Y”;
扫描仪键盘=新扫描仪(System.in);
System.out.print(“此程序生成乘法表”);
System.out.println(“基于您输入的数字”);
而(是非同等信号情况(“Y”))
{
System.out.println(“请输入一个数字”);
number=键盘.nextInt();
int行=1;
int列=1;
System.out.println(“|”+MAKETHISWORK+“\t”);//这就是我被卡住的地方。
System.out.println(“------------------”);
而(数量>0)
{     
行=1;

而(row为什么不直接使用另一个循环来构建一个字符串以达到该值

StringBuilder builder = new StringBuilder();
for (int i = 1; i <= number; i++) {
    builder.append(i).append(" ");
}
System.out.println(" |" + builder.toString() + "\t");
StringBuilder=新建StringBuilder();
对于(int i=1;i