简体   繁体   English

找到2个争论的最小值

[英]Find minimum value of 2 arguements

I cannot find the syntax error here. 我在这里找不到语法错误。 Can anyone please help me to find this ? 有人可以帮我找到这个吗?

Question is this - (2) Write a program to get 2 values as command line arguments and find the Minimum of those 2 using if-else. 问题是这个 - (2)编写一个程序来获取2个值作为命令行参数,并使用if-else找到那些2的最小值。

class MinNumber{ class MinNumber {

{
public static void main(String [] myArray){

    int length = myArray.length;

    if((myArray[0][0]) < (myArray[0][1])){
        System.out.println("Minimum number is : " + myArray[0][0]);
        }
    else{
        System.out.println("Minimum number is : " + myArray[0][1]);
        }
}

You can not compare String value with < since it is not primitive Also Array is single array not double array. 你不能将String值与<比较<由于它不是原始的而且Array也是单个数组而不是double数组。

Below is not safe code but simplest : 下面不是安全的代码,而是最简单的:

    if (Integer.parseInt(myArray[0]) < Integer.parseInt(myArray[1])) {
        System.out.println("Minimum number is : " + myArray[0]);
    } else {
        System.out.println("Minimum number is : " + myArray[1]);
    }

myArray[0][0] thats how you access elements of two dimensional array. myArray[0][0]就是如何访问二维数组的元素。 myArray[0] is enough for one dimensional array. myArray[0]足以用于一维数组。

So, you should parse the numbers out of myArray[0] and myArray[1] and compares them instead. 所以,你应该解析myArray[0]myArray[1]然后比较它们。

here is your problem. 这是你的问题。 in your main , notice the one dimension array? 在你的主要,注意一维数组?

public static void main(String [] myArray) 

In your body, you are accessing it as 2D array. 在您的身体中,您将其作为2D阵列进行访问。

if((myArray[0][0]) < (myArray[0][1])){
    System.out.println("Minimum number is : " + myArray[0][0]);
    }

By default ,The main method accepts a single argument: an array of elements of type String. 默认情况下,main方法接受一个参数:String类型的元素数组。 I am assuming that you actually meant to access the string from myArray ( as single dimension array) and proceed accordingly. 我假设你实际上是想从myArray访问字符串(作为单维数组)并相应地继续。

Also refer this if needed: http://docs.oracle.com/javase/tutorial/getStarted/application/index.html 如果需要,请参考: http//docs.oracle.com/javase/tutorial/getStarted/application/index.html

命令行采用的参数是字符串类型,首先需要将它们转换为整数或浮点数以进行数字比较。

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

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