简体   繁体   English

在Java中将字符串转换为10位整数

[英]Converting a string to a 10-digit integer in Java

I have this assignment which should check whether a Triangle is Isosceles, Scalene, Equilateral, Right-Angled. 我有此作业,应检查三角形是否为等腰,斜角,等边线,直角。

I am required to use only integers which are less or equal to 2,147,483,647 (or 2^31-1 in other words). 我只需要使用小于或等于2,147,483,647(或2 ^ 31-1)的整数。 I am obliged to use Linux and its Terminal to enter values and get results. 我必须使用Linux及其终端输入值并获取结果。

There is no problem with the output when I enter 1 to 9-digit integers, but if I want to conduct a test with a 10-digit integers such as 2^31-1, then I get a result which is not appropriate... 当我输入1到9位数字的整数时,输出没有问题,但是如果我想使用10位数字的整数(例如2 ^ 31-1)进行测试,那么我得到的结果是不合适的。 。

Here is a fragment of my code: 这是我的代码的一部分:

public static void main (String[] args)
{
    int a,b,c;
    a = Integer.parseInt(args[0]);
    b = Integer.parseInt(args[1]);
    c = Integer.parseInt(args[2]);
}

Could anyone suggest me how to deal with this issue? 有人可以建议我如何处理这个问题吗?

I don't know.. but I tried running your code, with BASH + JAVA and I don't see any problem with the output. 我不知道..但是我尝试使用BASH + JAVA运行您的代码,但我看不到输出有任何问题。

Here is the screenshot. 这是屏幕截图。

在此处输入图片说明


Ok now to solve the real problem(this must register at an emotional level) of if some triangle is actually an equilateral triangle, or scalene triangle or isosceles triangle or a right angle triangle. 现在可以解决一个实际问题(必须在情感层面进行记录),即某个三角形实际上是等边三角形,斜角三角形,等腰三角形还是直角三角形。

To solve a problem, write down the functional logic involved in the problem 要解决问题,请写下问题所涉及的功能逻辑

  1. Equilateral triangle :- A triangle for which, all three sides are equal 等边三角形:-三个边都相等的三角形
  2. Isosceles triangle :- A triangle for which, two of the three sides are equal 等腰三角形:-三角形,三个边中的两个相等
  3. Scalene triangle :- A triangle for which, no sides are equal 斜角三角形:-边均不相等的三角形

Now think of the constraints 现在想想约束

  1. You can only use integers 您只能使用整数
  2. Input sent as arguments 输入作为参数发送

Now for writing the program since you're done analyzing 现在,由于您已完成分析,现在可以编写程序了

public class BufferProblem{
   public static void main(String[] args){ 
     int a, b, c;
     a = Integer.parseInt(args[0]);
     b = Integer.parseInt(args[1]);
     c = Integer.parseInt(args[2]);

     if ((a == b) && (b == c)) { System.out.println("It is an equilateral triangle"); }
     else if (a == b || b == c || c == a) { System.out.println("It is an isosceles triangle"); }
     else if ((a != b) && (b != c) && (c != a)) { System.out.println("It is a scalene triangle");}

   }
}

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

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