2017-02-17 42 views
-3

我是初学者.....我不能似乎解决涉及多个方法这个问题....使用扫描仪的方法没有做任何事情解决不了这个:

package bucky; 

import java.util.Scanner; 

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

     input(); 
     for(int time=1;time<values[2];++time){ 

      double A=values[1]*Math.pow(1+values[3], time); 

     System.out.println("You have: "+A+ " subscribers today"); 
     }} 

     public static double[] input(){ 
      Scanner s=new Scanner(System.in); 
      double[] values=new double[3]; 



      System.out.println("Enter the Principal value: "); 
      values[1]=s.nextDouble(); 
      System.out.println("Enter the no. of days: "); 

      values[2]=s.nextInt(); 
      System.out.println("Enter the Rate of growth : "); 
      values[3]=s.nextDouble(); 
      return values; 
     } 
} 
+2

现在,正是你想做什么?你的问题没有任何明确说明请检查如何提出问题。 –

+2

问题是? – Lucero

+1

它不编译。 'values []'在主要方法中不可访问 –

回答

2

input()必须分配到values对不对?像:double[] values = input();

另外,数组中的索引以0 ...开头,所以values [0]是第一个值,依此类推。

尝试此代码:

public static void main(String args[]) 
{ 
    double[] values = input(); 
    for (int time = 1; time < values[2]; ++time) 
    { 
     double A = values[1] * Math.pow(1 + values[3], time); 

     System.out.println("You have: " + A + " subscribers today"); 
    } 
} 

public static double[] input() 
{ 
    Scanner s = new Scanner(System.in); 
    double[] values = new double[3]; 

    System.out.println("Enter the Principal value: "); 
    values[0] = s.nextDouble(); 
    System.out.println("Enter the no. of days: "); 

    values[1] = s.nextInt(); 
    System.out.println("Enter the Rate of growth : "); 
    values[2] = s.nextDouble(); 
    return values; 
} 
+0

你确定可以像这样调用静态方法'input'吗?它不需要'test.input()'(测试他定义的类)或删除静态限定符? –

+0

Yeap,只要它处于相同的上下文(类)中,就可以调用这样的静态方法。如果您删除了限定符,那么您将需要对象的实例来访问该方法。 – user218046

+0

嗯,你是对的。可能在不知情的情况下做到了。谢谢 –

0

尝试双[]值=输入();主要方法。

import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

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

    double[] values = input(); 
    for(int time=1;time<values[2];++time){ 

     double A=values[0]*Math.pow(1+values[2], time); 

    System.out.println("You have: "+A+ " subscribers today"); 
    }} 

    public static double[] input() 
    { 
     Scanner s=new Scanner(System.in); 
     double[] values=new double[3]; 



     System.out.println("Enter the Principal value: "); 
     values[0]=s.nextDouble(); 
     System.out.println("Enter the no. of days: "); 

     values[1]=s.nextInt(); 
     System.out.println("Enter the Rate of growth : "); 
     values[2]=s.nextDouble(); 
     return values; 
    } 

} 

同样它会抛出一个arrayoutofbondsexception ...改变values[1]values[0]values [2]values[1]values[3]values[2] ..