【问题标题】:Simple Java program that squares and cubes a users input number将用户输入的数字平方和立方的简单 Java 程序
【发布时间】:2015-10-27 20:38:51
【问题描述】:

我目前正在上大学的第一堂编程课,到目前为止,我已经非常了解如何编写基本的东西。这个最新的任务让我感到困惑和难过。

我的任务是:

  1. 接受用户的数字输入(整数值)
  2. 打印出输入数字的正方形和立方体。
  3. 确保数字大于 0。
  4. 重复上述操作 3 次。
  5. 如果输入的数字

我的问题是我不确定如何为此设置变量以及如何准确添加循环以重复该过程 3 次。

这就是我到目前为止所拥有的一切,不知道从哪里开始,任何帮助将不胜感激。谢谢

import java.io.*;

public class Assignment3 //class name here, same as file name
{
    public Assignment3() throws IOException{ //constructor, place class name here
        // use BufferedReader class to input from the keyboard
        // declare a variable of type BufferedReader
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

        //declare variable for input
        String inputString;

        // houseKeeping()
        String yourNumber;
        int number;
        int  totalSquare = 0;
        int totalCube = 0;
        int count;
        int badNumber=0;  
        String squareCube = " Your number squared is" +square +"your number cubed is"+cube;


        System.out.print("Enter a number: ");
        inputString = input.readLine();
        yourNumber = inputString;
    }//end constructor
}

public static void main(String [] args) throws IOException
{
    new Assignment3(); //class constructor name
} 

【问题讨论】:

  • 您不必使用循环。只需拨打new Assignment3() 3 次
  • 如果你想将字符串yourNumber转换成一个整数,这样你就可以将它平方和立方,使用Integer.parseIntdocs.oracle.com/javase/7/docs/api/java/lang/…
  • @PaulNikonowicz 如果用户输入了错误的号码,你想怎么办?
  • 为什么要投反对票?我们曾经都是初学者。
  • 你并不是真的在问一个问题,而是试图让别人为你写一个程序。对我来说,很明显你不知道如何解决这个问题。您应该阅读/学习更多基本的 Java 教程。

标签: java cube


【解决方案1】:

我可以在这里发布答案,但这无助于您学习:)

首先,考虑使用Scanner 来获取您的输入。使用BufferedReader 会将输入数据读取为String。可以使用Integer.parseInt()String 转换为int,但使用Scanner.nextInt() 更方便。

其次,关于循环:

既然要循环 3 次,下面的循环形式似乎比较合适:

for (int i=0; i<3; i++) {
  //read input, calculate square & cube
}

在循环内部,您需要检查无效条目(数字 break 提前跳出循环并结束程序。

【讨论】:

  • 感谢 NickJ 的回复,我将在晚上的大部分时间里工作,并将我的最终代码发布在这里。我知道我能做到,只是我的大脑还没有看到它。
【解决方案2】:

抱歉,格式不正确。我在网站上修复它的时间太长了...

    package com.ispecsoft.porkypig;

    import java.io.*;

    public class Assignment3 // class name here, same as file name
    {
        public Assignment3() throws Exception
        {

        }

        public static void DoIt()
        {

            try
            {
                int xIn = 0;
                BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

            for (int x = 0; x < 3; x++)
            {                
                 System.out.print("Enter a number: ");
                 xIn = Integer.parseInt(input.readLine());

                 int iSquare = xIn * xIn;
                 int iCube = iSquare * xIn;

                 System.out.println(x == 0 ? " Your number squared is ("+ iSquare + ") your number cubed is (" + iCube: ") Your number squared is (" + iSquare+ ") your number cubed is (" + iCube + ")");
            }

            }
            catch (NumberFormatException e)
            {
                System.out.print("The character's you entered are not integers.. this application will now close. ");
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e)
            {
                System.out.print("There was an IOException.. this application will now close. ");
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        public static void main(String[] args) throws Exception
        {
        Assignment3.DoIt();
        }
    }

【讨论】:

  • 谢谢,这也是我的想法,我们只是还没有真正了解xIn
  • xIn 只是一个变量名!
  • 如果你喜欢它,你至少可以投票吗?前几天我解决他们的问题时,有人投票否决了我……不确定到底是谁投了反对票,但是如果“当我为某人解决问题时……没有给予信用??”
猜你喜欢
  • 2020-04-13
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 2021-02-09
相关资源
最近更新 更多