简体   繁体   English

用限制和IF语句在Java中打印矩形不起作用

[英]Printing a Rectangle in Java with limits and IF statement that isn't working

I am trying to create a program that takes user input and makes a rectangle. 我正在尝试创建一个需要用户输入并制作一个矩形的程序。 The user input to numbers for length and width and another symbol or character to fill in the rectangle. 用户输入数字以表示长度和宽度,并输入另一个符号或字符以填充矩形。 That all works fine. 一切正常。 My code works for creating a rectangle of any size but I need limit If the no of rows or cols is less than or equal to zero or more than 50 it needs to print out "error in input". 我的代码可用于创建任何大小的矩形,但是我需要限制如果行或列的数量小于或等于零或大于50,则需要打印出“输入错误”。

This is the code I have and I am currently trying to define it with if statement and it keeps print the state "error in input and the rectangle. Any help would be appreciated or another method to achieve the same thing. 这是我所拥有的代码,我目前正在尝试使用if语句对其进行定义,并且该代码始终将状态“在输入和矩形中出现错误”打印出来。任何帮助或其他实现相同目的的方法都将受到赞赏。

import java.util.Scanner;

public class Rectangle
  {
    public static void main(String[] args)
  {
 int length;
 int width;
 String symbol;
 char symbolz;
 int xLength;
 int yWidth;



 Scanner input = new Scanner(System.in);         
 while(input.hasNext()){
    {  width = input.nextInt();
       length = input.nextInt();
       symbol = input.next();
       symbolz = symbol.charAt(0);


       if (length<=0||length>50||width<=0||width>50){
       }
        System.out.print("error in input");{

       {for (yWidth = 1; yWidth <= width; yWidth++){
        for (xLength = 1; xLength <= length; xLength++) {
            System.out.print(symbolz);
        }

        System.out.println(" ");
    }
    for(yWidth = 1; yWidth >= width; yWidth--){
        for (xLength = 1; xLength >= length; xLength--){
            System.out.print(symbolz);
        }

        System.out.println(" ");
                }
            }
            }
        }
    }
}}

Edited: switched to if statement but still am having trouble because it keeps inserting "error in user input" in all things typed and also prints the rectangle when it isn't supposed to. 编辑:切换到if语句,但仍然有麻烦,因为它会在所有键入的内容中不断插入“用户输入错误”,并在不应该显示的情况下打印矩形。

So far I have tried to get the If statement to work and it keeps doing the same thing where it keeps print "error in user input" on inputs that are out side the range but it also prints the rectangle after which it shouldn't do. 到目前为止,我一直试图使If语句起作用,并且它会继续执行相同的操作,即在超出范围的输入上始终显示“用户输入错误”,但它还会打印矩形,此后不应执行。 Any guidance? 有指导吗?

The construct is called a do-while, and the general format is: 该构造称为do-while,其通用格式为:

do {
    // statements
} while (condition);

Your do loop is missing the while (condition) which indicates when the loop should stop. 您的do循环缺少while (condition)while (condition)指示循环应在何时停止。

It's essentially a while loop, with the difference being that with do-while, condition is tested at the end of the loop, where with a while loop, it's tested at the start, so a do-while will always execute the loop at least once. 本质上是一个while循环,不同之处在于do-while的条件是在循环的末尾进行测试的,而while循环的条件是在开始时进行测试的,因此do-while至少将始终执行循环一旦。

I got it to work with a if and continue statement. 我让它与if和Continue语句一起使用。

import java.util.Scanner;

public class Rectangle
 {
 public static void main(String[] args)
 {
 int length;
 int width;
 String symbol;
 char symbolz;
 int xLength;
 int yWidth;



 Scanner input = new Scanner(System.in);
 while(input.hasNext()){
    {  width = input.nextInt();
       length = input.nextInt();
       symbol = input.next();
       symbolz = symbol.charAt(0);
       if (length<=0||length>50||width<=0||width>50){
       System.out.print("error in input");
       System.out.println();
       continue;}
   {
       for (yWidth = 1; yWidth <= width; yWidth++){
            for (xLength = 1; xLength <= length; xLength++) {
                System.out.print(symbolz);

    }

    System.out.println(" ");
}
for(yWidth = 1; yWidth >= width; yWidth--){
    for (xLength = 1; xLength >= length; xLength--){
         System.out.print(symbolz);
    }

    System.out.print(" ");
            }
        }
        }
    }
}

} }

Error is here : 错误在这里:

  if (length<=0||length>50||width<=0||width>50){
       }    // --> Error: if input outside range then we have to use continue or exit.
        System.out.print("error in input");{   // Message must be in previous if 

use This: 用这个:

  if((length<=0 || length >50) || (width<=0 || width>50))
       {
         System.out.println("Error in input");
       } 

Hope this may solve your problem. 希望这可以解决您的问题。

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

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