2017-02-17 44 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] ..