我在将用户输入传递到数组时遇到问题。我应该接受用户输入并根据类型将其传递到已定义的数组中,然后将其传递给一个给出数组总数的方法。问题是总数显示为" O",这让我相信数据不在数组中。任何建议都会有所帮助。
import java.util.Scanner;
public class WestonPASS9
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String input1;
int size1;
int enterint;
double enterdouble;
float enterfloat;
long enterlong;
int[] arrayint = new int[0];
double[] arraydouble = new double[0];
float[] arrayfloat = new float[0];
long[] arraylong = new long[0];
System.out.println("What data type are you using? (Int, Double, Float, Long)");
input1 = keyboard.next();
System.out.println("How many numbers will you be entering?");
size1 = keyboard.nextInt();
if(input1.equals("Int"))
{
arrayint = new int[size1];
for(int i=0;i<size1;i++){
System.out.println("Enter Integer");
enterint = keyboard.nextInt();}
getTotal(arrayint);
}
if(input1.equals("Double"))
{
arraydouble = new double[size1];
for(int i=0;i<size1;i++){
System.out.println("Enter Double");
enterdouble = keyboard.nextDouble();}
}
if(input1.equals("Float"))
{
arrayfloat = new float[size1];
for(int i=0;i<size1;i++){
System.out.println("Enter Float");
enterfloat = keyboard.nextFloat();}
}
if(input1.equals("Long"))
{
arraylong = new long[size1];
for(int i=0;i<size1;i++){
System.out.println("Enter Long");
enterlong = keyboard.nextLong();}
}
}
public static int getTotal(int[] x)
{
int total = 0;
for (int index = 0; index < x.length; index++)
{
total += x [index];
}
System.out.println("Total: "+total);
return total;
}
}
答案 0 :(得分:0)
我明白了,如果有人需要答案,这是代码。
import java.util.Scanner;
public class WestonPASS9
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String input1;
int size1;
int enterint;
double enterdouble;
float enterfloat;
long enterlong;
int[] arrayint;
double[] arraydouble;
float[] arrayfloat;
long[] arraylong;
System.out.println("What data type are you using? (Int, Double, Float, Long)");
input1 = keyboard.next();
System.out.println("How many numbers will you be entering?");
size1 = keyboard.nextInt();
if(input1.equals("Int"))
{
arrayint = new int[size1];
for(int index =0; index < arrayint.length; index++)
{
System.out.println("Enter Integer " + (index + 1) + ": ");
arrayint[index] = keyboard.nextInt();
}
for (int index = 0; index < size1; index++)
System.out.print(arrayint[index]+" ");
getTotal(arrayint);
getAverage(arrayint);
getHighest(arrayint);
getLowest(arrayint);
}
if(input1.equals("Double"))
{
arraydouble = new double[size1];
for(int index =0; index < size1; index++)
{
System.out.println("Enter Double " + (index + 1) + ": ");
arraydouble[index] = keyboard.nextDouble();
}
for (int index = 0; index < size1; index++)
System.out.print(arraydouble[index]+" ");
getTotal1(arraydouble);
getAverage1(arraydouble);
getHighest1(arraydouble);
getLowest1(arraydouble);
}
if(input1.equals("Float"))
{
arrayfloat = new float[size1];
for(int index =0; index <size1; index++)
{
System.out.println("Enter Float " + (index + 1) + ": ");
arrayfloat[index] = keyboard.nextFloat();
}
for (int index = 0; index < size1; index++)
System.out.print(arrayfloat[index]+" ");
getTotal2(arrayfloat);
getAverage2(arrayfloat);
getHighest2(arrayfloat);
getLowest2(arrayfloat);
}
if(input1.equals("Long"))
{
arraylong = new long[size1];
for(int index =0;index <size1; index++)
{
System.out.println("Enter Long" + (index + 1) + ": ");
arraylong[index] = keyboard.nextLong();
}
for (int index = 0; index < size1; index++)
System.out.print(arraylong[index]+" ");
getTotal3(arraylong);
getAverage3(arraylong);
getHighest3(arraylong);
getLowest3(arraylong);
}
}
public static int getTotal(int[] x)
{
int total = 0;
for (int index = 0; index < x.length; index++)
{
total += x [index];
}
System.out.println("Total: "+total);
return total;
}
public static double getAverage(int[] x)
{
int total = 0;
for (int index = 0; index < x.length; index++)
{
total += x [index];
}
double average = (total/2.0);
System.out.println("Average: "+average);
return average;
}
public static int getHighest (int[] x)
{
int highest = x[0];
for (int index=1; index<x.length; index++)
{
if (x[index]>highest)
highest=x[index];
}
System.out.println("Highest: "+highest);
return highest;
}
public static int getLowest (int[] x)
{
int lowest = x[0];
for (int index=1; index<x.length; index++)
{
if (x[index]<lowest)
lowest=x[index];
}
System.out.println("Lowest: "+lowest);
return lowest;
}
public static double getTotal1(double[] x)
{
double total = 0;
for ( int index = 0; index < x.length; index++)
{
total += x [index];
}
System.out.println("Total: "+total);
return total;
}
public static double getAverage1(double[] x)
{
double total = 0;
for (int index = 0; index < x.length; index++)
{
total += x [index];
}
double average = (total/2.0);
System.out.println("Average: "+average);
return average;
}
public static double getHighest1 (double[] x)
{
double highest = x[0];
for (int index=1; index<x.length; index++)
{
if (x[index]>highest)
highest=x[index];
}
System.out.println("Highest: "+highest);
return highest;
}
public static double getLowest1 (double[] x)
{
double lowest = x[0];
for (int index=1; index<x.length; index++)
{
if (x[index]<lowest)
lowest=x[index];
}
System.out.println("Lowest: "+lowest);
return lowest;
}
public static float getTotal2(float[] x)
{
float total = 0;
for ( int index = 0; index < x.length; index++)
{
total += x [index];
}
System.out.println("Total: "+total);
return total;
}
public static double getAverage2(float[] x)
{
float total = 0;
for (int index = 0; index < x.length; index++)
{
total += x [index];
}
double average = (total/2.0);
System.out.println("Average: "+average);
return average;
}
public static float getHighest2 (float[] x)
{
float highest = x[0];
for (int index=1; index<x.length; index++)
{
if (x[index]>highest)
highest=x[index];
}
System.out.println("Highest: "+highest);
return highest;
}
public static float getLowest2 (float[] x)
{
float lowest = x[0];
for (int index=1; index<x.length; index++)
{
if (x[index]<lowest)
lowest=x[index];
}
System.out.println("Lowest: "+lowest);
return lowest;
}
public static long getTotal3(long[] x)
{
long total = 0;
for ( int index = 0; index < x.length; index++)
{
total += x [index];
}
System.out.println("Total: "+total);
return total;
}
public static double getAverage3(long[] x)
{
long total = 0;
for (int index = 0; index < x.length; index++)
{
total += x [index];
}
double average = (total/2.0);
System.out.println("Average: "+average);
return average;
}
public static long getHighest3 (long[] x)
{
long highest = x[0];
for (int index=1; index<x.length; index++)
{
if (x[index]>highest)
highest=x[index];
}
System.out.println("Highest: "+highest);
return highest;
}
public static long getLowest3 (long[] x)
{
long lowest = x[0];
for (int index=1; index<x.length; index++)
{
if (x[index]<lowest)
lowest=x[index];
}
System.out.println("Lowest: "+lowest);
return lowest;
}
}