Java阵列/程序问题

时间:2015-10-15 05:32:58

标签: java arrays

好的,所以我正在为我的大学课程开发一个java程序,现在我花了很多时间试图弄清楚我做错了什么。

我的节目如下。它需要做的是将整数转换为单个数字,然后将它们全部添加。它必须显示原始数字,个别数字,然后是总和。

项目的一部分是它必须接受负数,然后显示正数和总和,但是对于我的数组,当输入负数时,它显示-1作为第一个数字,我不能用于生命我弄清楚如何解决它。

示例:-3456的输入最终显示-1,3,4,5,6和17的总和,这显然是错误的。

非常感谢任何帮助,谢谢!

import java.util.*;
import javax.swing.JOptionPane; //import package for using dialog boxes
import java.util.Arrays; //import package for arrays

public class Project4
{
   public static void main(String args[])
   {   

//declares and initialize variables sum & counter
      int sum = 0;
      int counter = 1;



//asks for integer input and stores as a string in numInput
      String numInput = JOptionPane.showInputDialog(null, "Enter an integer: ", "User Input", JOptionPane.QUESTION_MESSAGE); 
      int input = Integer.parseInt(numInput);//parses the value of numInput as an integer and stores as input

      int numLength = String.valueOf(input).length();//sets numLength as the length of input
      int [] varArray = new int[numLength];//initilizes an array to match numLength

      if(input == (-input))//tests for negative input value
      {

         input = (input * (-1));//corrects the negative input value

            for (int i = 0; i < numLength; i++ ) //starts a for loop
            {

               String var = numInput.substring(i,counter);//stores the value of the number at the location between i and counter as var

               int numVal = Character.getNumericValue(var.charAt(0));//sets numVal to the numeric value of the character at 0

               varArray[i] = numVal;//saves the numVal to the array at position i

               sum = sum + numVal;//adds the sum of the numbers as the loop goes

               counter++;//increments the counter
            }
      }

      else //starts alternate loop if input was not a negative value
      {         
            for (int i = 0; i < numLength; i++ ) 
            {

               String var = numInput.substring(i,counter);

               int numVal = Character.getNumericValue(var.charAt(0));

               varArray[i] = numVal;

               sum = sum + numVal;

               counter++;

            }
      }

      JOptionPane.showMessageDialog(null, "The Digits of Integer Entered " + input + " are: " + Arrays.toString(varArray).replace("[", "").replace("]", "") + "\nThe sum is: " + sum, "NUMBERS", JOptionPane.INFORMATION_MESSAGE);
      System.exit(0); //exits program and is required when using GUI

   }
}

4 个答案:

答案 0 :(得分:3)

if(input < 0)

不测试负输入,它测试输入是否为0。

erb :index, :layout => !request.xhr?

测试负面输入。

答案 1 :(得分:1)

始终避免使用if。

input = Math.abs( input );

这会照顾标志,并且不需要if。

虽然我在这,但这是计算数字总和的首选方式(假设您不需要将数字存储在数组中,从左到右):

int sum = 0;
while( input > 0 ){
    sum += input%10;
    input /= 10;
}

答案 2 :(得分:0)

替换条件

if(input == (-input))

通过

if(Math.signum(input) == -1.0) 

或者

if(input < 0) 

答案 3 :(得分:0)

try {
  int integerNumber = Integer.parseInt(input);//It may positive or negative
  if(integerNumber > 0) {
      //Do the stuff positive number
  } else if(integerNumber < 0){
       integerNumber = (integerNumber * -1);
      //Do the stuff for negative number

  } else {
     System.out.println("Enter Number is zero ::");   
  }
}catch(NumberFormatException nfe) {
  System.out.println("Please Enter a Integer Number :::"+input);
}

//I think this code will help for you.