在 Java 中返回字符串方法?

ncc*_*ows 5 java string methods

我正在上一个初级编程课程,直到现在我们已经开始使用方法,而且我不确定我是否完全理解“静态”、“无效”、和“返回”语句。

特别是对于这个任务,我以为我已经全部弄清楚了,但是它在 main 方法的行上说它“找不到符号直方图”,尽管我显然是从另一个方法返回它。有谁能帮帮我吗?

作业:“你看到你在编写程序时可能经常需要直方图,所以你决定让这个程序使用你的程序 310a2 直方图。你可以添加到这个程序或将它用作一个类。你还将编写一个类(或方法) ) 将生成各种范围内的随机数。您可能希望星号代表不同的值(1、100 或 1000 个单位)。您可能还希望使用星号以外的字符(例如 $)来表示单位运行程序足够多的次数来说明程序的各种能力。

所需语句:输出、循环控制、决策、类(可选)、方法。

示例输出:

十月销售

日日销售额图

2 37081 *************************************

3 28355 ****************************

4 39158 ****************************************

5 24904 ************************

6 28879 ****************************

7 13348 *************”

这是我所拥有的:

import java.util.Random;

public class prog310t
{ 
  public static int randInt(int randomNum) //determines the random value for the day
  {   
    Random rand = new Random();

    randomNum = rand.nextInt((40000 - 1000) + 1) + 10000;

    return randomNum;
  }

  public String histogram (int randomNum) //creates the histogram string
  {
    String histogram = "";
    int roundedRandom = (randomNum/1000);
    int ceiling = roundedRandom;
    for (int k = 1; k < ceiling; k++)
    {
      histogram = histogram + "*";
    }

    return histogram;   
  }

  public void main(String[] Args)
  {
    System.out.println("Sales for October\n");

    System.out.println("Day        Daily          Sales Graph");

    for (int k = 2; k < 31; k++)
    {
      if (k == 8 || k == 15 || k == 22 || k == 29)
      {
        k++;
      }

      System.out.print(k + "         ");

      int randomNum = 0;

      randInt(randomNum);

      System.out.print(randomNum + "       ");

      histogram (randomNum);

      System.out.print(histogram + "\n");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑:多亏了你们,现在我已经弄明白静态是什么意思了。现在我有一个新问题;程序运行,但直方图返回为空。有人可以帮我理解为什么吗?新代码:

import java.util.Random;

public class prog310t
{ 
  public static int randInt(int randomNum) //determines the random value for the day
  {   
    Random rand = new Random();

    randomNum = rand.nextInt((40000 - 1000) + 1) + 10000;

    return randomNum;
  }

  public static String histogram (int marketValue) //creates the histogram string
  {
    String histogram = "";
    int roundedRandom = (marketValue/1000);
    int ceiling = roundedRandom;
    for (int k = 1; k < ceiling; k++)
    {
      histogram = histogram + "*";
    }

    return histogram;   
  }

  public static void main(String[] Args)
  {
    System.out.println("Sales for October\n");

    System.out.println("Day        Daily          Sales Graph");

    for (int k = 2; k < 31; k++)
    {
      if (k == 8 || k == 15 || k == 22 || k == 29)
      {
        k++;
      }

      System.out.print(k + "         ");

      int randomNum = 0;

      int marketValue = randInt(randomNum);

      System.out.print(marketValue + "       ");

      String newHistogram = histogram (randomNum);

      System.out.print(newHistogram + "\n");
    }
  }


}
Run Code Online (Sandbox Code Playgroud)

Msh*_*nik 4

你是对的,你的问题根源在于不理解static。关于这一点有很多资源,但这里足以说明某些东西static属于一个,而某些东西不是静态的属于一个特定的实例。这意味着

public class A{
    public static int b;

    public int x;
    public int doStuff(){
        return x;
    }

    public static void main(String[] args){
        System.out.println(b); //Valid. Who's b? A (the class we are in)'s b.
        System.out.println(x); //Error. Who's x? no instance provided, so we don't know.
        doStuff(); //Error. Who are we calling doStuff() on? Which instance?

        A a = new A();
        System.out.println(a.x); //Valid. Who's x? a (an instance of A)'s x.
    }
}
Run Code Online (Sandbox Code Playgroud)

因此与您的方法histogram无关static,因此您需要一个实例来调用它。但你不应该需要一个实例;只需将该方法设为静态即可:

改成。public String histogram(int randomNum)public static String histogram(int randomNum)

完成后,该行histogram(randomNum);变得有效。但是,您仍然会收到错误System.out.print(histogram + "\n");,因为histogram此处定义的是函数,而不是变量。这与声明有关return。当某件事说return x(对于 的任何值x)时,它是说终止当前的方法调用并将该值传递x给调用该方法的人。

例如,考虑表达式2 + 3. 如果你要说int x = 2 + 3,你会期望事后x有价值5。现在考虑一个方法:

public static int plus(int a, int b){
    return a + b;
}
Run Code Online (Sandbox Code Playgroud)

以及声明:int x = plus(2, 3);。同样在这里,我们希望之后x有价值5。计算完成,等待该值(类型int)的任何人都会接收并使用该值,但是将使用该类型的单个值来代替它。例如:

int x = plus(plus(1,2),plus(3,plus(4,1));->x值为 11。

回到您的示例:您需要将一个变量分配给从 返回的 String 值histogram(randomNum);,如下所示:

改成。histogram(randomNum)String s = histogram(randomNum)

这将使所有内容都能编译,但您将遇到最后一个障碍:该东西将无法运行!这是因为可运行main方法必须是静态的。因此,更改您的 main 方法以具有签名:

public static void main(String[] args){...}
Run Code Online (Sandbox Code Playgroud)

然后点击绿色按钮!