简体   繁体   English

Java程序未打印我的所有打印语句

[英]Java program not printing all of my print statements

In my computer science class we were asked to create a program that would prompt the user to enter how many rows, columns, and what symbol they would like printed in a "magic box" and to store each of these variables and print them their very own magic box using a nested for loop. 在我的计算机科学课上,我们被要求创建一个程序,该程序将提示用户输入要在“魔术盒”中打印多少行,列和什么符号,并存储每个变量并打印它们自己的魔术盒,使用嵌套的for循环。 My program compiles properly and prints the correct box based on my inputs, but it does not print two of my print statements. 我的程序正确编译并根据我的输入打印正确的框,但它不打印我的两个打印语句。 It will print the first three statements (the ones prompting the user to enter certain things), but does not print the statements 它将打印前三个语句(提示用户输入某些内容的语句),但不打印语句

Here comes the magic...Here's your very own magic box

and

This magic box brought to you by Beth Tanner."

I have tried everything I can think of and still cannot get these statements to print, any help would be greatly appreciated. 我已尽力想尽一切办法,但仍无法打印这些声明,我们将不胜感激。 I am including my program below. 我在下面包括我的程序。

import java.util.Scanner;

public class MagicBox {
  public static void main(String[] args) {
    Scanner input= new Scanner(System.in);

    System.out.println("How many rows would you like in your box?");
      int rows = input.nextInt();
    System.out.println("How many columns would you like in your box?");
      int columns = input.nextInt();
    System.out.println("What symbol would you like in your box?");
      String symbol = input.next(); 

    System.out.println("Here comes the magic...\nHere's your very own magic box!");

    int count1;
    int count2;
      for(count1 = 1; count1 <= rows; count1++) 
        for (count2 = 1; count2 <= columns; count2++)
          System.out.print(symbol);
          System.out.println(); 
      System.out.println("This magic box brought to you by Beth Tanner.");   

  } // end main
} // end class

Using correct blocks, everything works. 使用正确的块,一切正常。

Note that the outer loop must enclose the newline produced by System.out.println(); 注意,外部循环必须包含由System.out.println();产生的换行符System.out.println(); . In your code this newline is only printed afer all row * columns symbols have been printed on one line. 在你的代码这个新行只印AFER 所有 row * columns符号被印上一条线。

int rows = 5;
int columns = 3;
String symbol = "@";

System.out.println("Here comes the magic...\nHere's your very own magic box!");

for (int count1 = 1; count1 <= rows; count1++) {
    for (int count2 = 1; count2 <= columns; count2++) {
        System.out.print(symbol);
    }
    System.out.println();
}

System.out.println("This magic box brought to you by Beth Tanner.");

Output: 输出:

Here comes the magic...
Here's your very own magic box!
@@@
@@@
@@@
@@@
@@@
This magic box brought to you by Beth Tanner.

I have no idea what Magic box is, but I assume you want something like this: 我不知道什么是魔术盒,但我认为您想要这样的东西:

for(count1 = 1; count1 <= rows; count1++) {
    for (count2 = 1; count2 <= columns; count2++) {
      System.out.print(symbol);
    }
    System.out.println();
  }

There are a couple of problems with your initial code: 您的初始代码有两个问题:

  • There is no "collumns" variable declared - you have a typo there and it's actually columns 没有声明“ collumns”变量-那里有错字,实际上是列
  • Always use curly braces in loops. 始终在循环中使用花括号。 Without those you only one expression will be executed in each loop, which means that System.out.println() will only be invoked once as opposed to being added after every row. 如果没有这些,则在每个循环中将只执行一个表达式,这意味着System.out.println()只会被调用一次,而不是在每行之后添加。

I think you want this: 我想你想要这个:

for(count1 = 1; count1 <= rows; count1++){ 
  for (count2 = 1; count2 <= columns; count2++)
    System.out.print(symbol);

  System.out.println(); 
}

or even clearer 甚至更清晰

for(count1 = 1; count1 <= rows; count1++){ 
  for (count2 = 1; count2 <= columns; count2++){
    System.out.print(symbol);
  }

  System.out.println(); 
}

This will give you the magic box you desire. 这将为您提供您想要的魔术盒。

This link may help you see the difference: stackoverflow question on omitting braces 该链接可能帮助您看到差异: 省略花括号的stackoverflow问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM