如何使用Java函数编写确定用户输入的4个整数是正方形还是矩形的程序? [英] How do I use Java functions to write a program that determines whether 4 integers entered by a user form a square or a rectangle?

查看:282
本文介绍了如何使用Java函数编写确定用户输入的4个整数是正方形还是矩形的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名学习计算机编程在线课程的学生.到目前为止,我似乎在整个春季学期都表现良好.但是,本周我被分配了作业,目的是测试我对Java函数的了解.我应该写一个程序,要求用户输入4个整数,然后确定这4个条目是正方形还是矩形.如果这4个条目确实是正方形或矩形,则假定的程序必须计算其周长.

I am a student studying an online course in computer programming. So far, I seem to be doing well throughout the Spring semester. However, I was assigned homework this week that is designed to test my knowledge of Java functions. I am supposed to write a program that asks a user to enter 4 integers, then determines whether those 4 entries form a square or a rectangle. If the 4 entries do make a square or rectangle, then the supposed program has to calculate its perimeter.

输出应该符合以下标准:

The output is supposed to adhere to the standards below:

示例1:

进入第1面

5

进入第二面

3

进入第3面

3

进入第4面

5

形成一个周长为16的矩形

Forms a rectangle with perimeter of 16

示例2:

进入第1面

5

进入第二面

5

进入第3面

5

进入第4面

5

形成一个周长为20的正方形

Forms a square with perimeter of 20

示例3:

进入第1面

5

进入第二面

6

进入第3面

7

进入第4面

8

不形成矩形或正方形.

我重读了几次作业问题,并尝试了学习本周所有课堂作业后能记住的一切.到目前为止,我只能在这样一个程序上草拟一半的工作:

I reread the assignment questions a couple of times and tried out whatever I could remember from studying all the classwork for this week. So far, I could only draft half of my work on such a program:

import java.io.*;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
    getUserInput();
    int a;
    int b;
    int c;
    int d;
int perimeter;
    boolean square = isSquare();
    boolean rect = isRectangle;
    printPerimeter();
}

System.out.println("Enter side 1: ");
a = Integer.parseInt(reader.readLine());

System.out.println("Enter side 2: ");
b = Integer.parseInt(reader.readLine());

System.out.println("Enter side 3: ");
c = Integer.parseInt(reader.readLine());

System.out.println("Enter side 4: ");
d = Integer.parseInt(reader.readLine());

if (a == c && b == d) {
    System.out.println("It is a rectangle.");
perimeter += side;
}

else if (a == b && b == c && c == d && d == a) {
System.out.println("It is a square.");
perimeter += side;
}
else {
    System.out.println("Incorrect. Try again.");
}

private void getUserInput() {
    System.out.println("Getting user input");
}

private boolean isSquare() {
    System.out.println("Is square?");
}

private boolean isRectangle() {
    System.out.println("Is rectangle?");
   }

private void printPerimeter() {
    System.out.println("Printing perimeter");
   }
}

现在,我需要弄清楚如何首先使代码在没有函数的情况下工作.然后,我必须将代码分解为功能.我如何从一个程序捕获用户的四个侧面并计算周长?

Now I need to figure out how to get the code working without functions first. Then, I have to break the code out into functions. How do I start with a program that captures 4 sides of the object from the user and calculates the perimeter?

谢谢您的时间和考虑.

推荐答案

这不是您所提问题的实际答案,但这是我一段时间以来一直想发布的内容.问题是:当您完全卡住时该怎么办.

This isn't an actual answer to your question, but it is something I've been meaning to post for a while. The problem is: what to do when you're completely stuck.

我在上大学的时候就学到的一件事是尝试概述代码,而不是专注于细节.在尝试找出困难的问题时,在代码中产生大纲并保持代码的同时编译和工作可以帮助您取得进步.

One thing I learned in college way back was to try to outline the code, rather than try to concentrate on details. Producing an outline in code and keeping the code compiling and working at the same time can help you make progress as you attempt to figure out a difficult problem.

从这样的东西开始:

public class SquareOrRectangle {
  public static void main( String... args ) {
    // 1. Get user input
    // 2. Determine if square
    // 3. Determine if rectangle
    // 4. Print perimeter
  }
}

就是这样.只需从基本内容开始,其中包含您认为需要的所有步骤.然后,开始进一步分解,使代码仍可编译并运行.

That's it. Just start with something basic that contains all the steps you think you need. Then, start to break this down further, keeping the code still compilable and running.

public class SquareOrRectangle {
  public static void main( String... args ) {
    // 1. Get user input
    getUserInput();

    // 2. Determine if square
    boolean square = isSquare();

    // 3. Determine if rectangle
    boolean rect = isRectangle();

    // 4. Print perimeter
    printPerimeter();

  }

  private void getUserInput() {
    System.out.println( "Getting user input" );
  }

  private boolean isSquare() {
    System.out.println( "Is square?" );
  }

  private boolean isRectangle() {
    System.out.println( "Is rectangle?" );
  }

  private void printPerimeter() {
    System.out.println( "Printing perimeter" );
  }
}

该程序仍然不执行任何操作,但是将问题分解为更易于解决的较小步骤.重要的是,该程序仍然可以编译并运行.这意味着您可以对其进行测试.这很重要,因为您不想编写大量的代码然后必须一次测试所有代码,并且希望能够测试更小的步骤,以便更快地发现错误.

The program still doesn't DO anything, but it breaks the problem into smaller steps that are easier to solve. Importantly, the program still compiles and runs. This means you can test it. This is important because you don't want to write a huge blob of code and then have to test it all at once, you want to be able to test smaller steps so that you can find errors more quickly.

如果在任何步骤中您都不知道如何解决该步骤,请将其再次分解为小步骤,然后重复进行,直到步骤足够小以至于您确实知道如何解决这些问题.

If at any step you don't know how to solve that step, break it down again into small steps, and repeat until you have steps small enough that you do know how to solve them.

就是这样.将您的程序分成几个小步骤.在尝试使其余代码正常工作之前,请测试每个步骤并使其正常运行.用较小的块编写测试和代码,不要一口气编写整个程序.

That's about it. Break your program into small steps. Test each step and get it working before trying to get the rest of the code working. Write tests and code in smaller blocks, don't try to write the whole program in one go.

这篇关于如何使用Java函数编写确定用户输入的4个整数是正方形还是矩形的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆