Java帮助:使用循环在开始arg和结束arg之间打印值的平方 [英] Java help: using loops to print the squares of values in between start arg and end arg

查看:97
本文介绍了Java帮助:使用循环在开始arg和结束arg之间打印值的平方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是java的新手,请原谅任何凌乱的代码等.

So I'm new to java, please excuse any messy code etc.

我必须编写一个程序,打印出在命令行中声明的开始和结束参数的平方值,以及之间的值的平方.

I have to make a program that prints out the squared value of a start and end argument declared in the command line, as well as the squares of the values in between.

这是我到目前为止所拥有的代码,非常粗糙,但是我需要帮助才能获得介于两者之间的变量以进行打印.

This is the code I have so far, it's very rough, but I need help getting the variables in between to print out.

public static void main(String[] args)
   {
      int start;
      int end;
      int start2;
      int end2;

      start  = Integer.parseInt(args[0]);
      end    = Integer.parseInt(args[1]);
      start2 = start*start;
      end2   = end*end;

      if (args.length == 2) {
           for (int i = start; i <= end; i++){
                   System.out.println("The square of "+start+" is "  +start2);
                   System.out.println("The square of "+end+  " is "  +end2);
                   return;
                }
  }

非常感谢您的帮助!

推荐答案

您可以在循环的内部中计算开始到结束的平方:

You can calculate the square of start to end inside the loop:

public static void main(String[] args)
{
    int start  = Integer.parseInt(args[0]);
    int end    = Integer.parseInt(args[1]);

    if (args.length == 2)
        for (int i = start; i <= end; i++)
            System.out.println("The square of " + i + " is "  + i*i);
}

这篇关于Java帮助:使用循环在开始arg和结束arg之间打印值的平方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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