返回Java中的字符串方法?

返回Java中的字符串方法?,java,string,methods,Java,String,Methods,我在一个初级编程课程中,在这之前,我对这些都很有意义,我们已经开始使用方法,我不完全确定我是否理解“static”、“void”和“return”语句 特别是对于这个任务,我认为我已经把它全部弄明白了,但是它在main方法的行上说“找不到符号直方图”,尽管我显然是从另一个方法返回的。有人能帮我吗 作业:“您发现在编写程序时可能经常需要直方图,因此您决定让此程序使用您的程序310a2直方图。您可以将其添加到此程序或将其用作一个类。您还将编写一个类(或方法)这将生成不同范围内的随机数。您可能希望星号

我在一个初级编程课程中,在这之前,我对这些都很有意义,我们已经开始使用方法,我不完全确定我是否理解“static”、“void”和“return”语句


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

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

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

样本输出:

十月份的销售额

每日销售图

237081*************************************

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

439158***************************************

524904************************

62879****************************

713348************* "

以下是我所拥有的:

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");
    }
  }
}
import java.util.Random;
公共类程序310T
{ 
public static int randInt(int randomNum)//确定当天的随机值
{   
Random rand=新的Random();
randomNum=rand.nextInt((40000-1000)+1)+10000;
返回随机数;
}
公共字符串直方图(int randomNum)//创建直方图字符串
{
字符串直方图=”;
int roundedRandom=(randomNum/1000);
内部天花板=圆形随机;
对于(int k=1;k<天花板;k++)
{
直方图=直方图+“*”;
}
返回直方图;
}
公共void main(字符串[]参数)
{
System.out.println(“10月份的销售额”);
System.out.println(“日销售图”);
对于(int k=2;k<31;k++)
{
如果(k==8 | | k==15 | | k==22 | | k==29)
{
k++;
}
系统输出打印(k+“”);
int randomNum=0;
randInt(randomNum);
系统输出打印(随机数+“”);
直方图(随机数);
系统输出打印(直方图+“\n”);
}
}
}
编辑:多亏了你们,现在我明白了静态的含义。现在我有了一个新问题;程序运行,但直方图返回为空。有人能帮我理解为什么吗?新代码:

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");
    }
  }


}
import java.util.Random;
公共类程序310T
{ 
public static int randInt(int randomNum)//确定当天的随机值
{   
Random rand=新的Random();
randomNum=rand.nextInt((40000-1000)+1)+10000;
返回随机数;
}
公共静态字符串直方图(int marketValue)//创建直方图字符串
{
字符串直方图=”;
int roundedRandom=(市场价值/1000);
内部天花板=圆形随机;
对于(int k=1;k<天花板;k++)
{
直方图=直方图+“*”;
}
返回直方图;
}
公共静态void main(字符串[]Args)
{
System.out.println(“10月份的销售额”);
System.out.println(“日销售图”);
对于(int k=2;k<31;k++)
{
如果(k==8 | | k==15 | | k==22 | | k==29)
{
k++;
}
系统输出打印(k+“”);
int randomNum=0;
int marketValue=randInt(randomNum);
系统输出打印(市场价值+“”);
字符串newHistogram=直方图(randomNum);
系统输出打印(新直方图+“\n”);
}
}
}

在调用
historogrom(randomNum)
之前,您需要将直方图设置为静态,或者声明将直方图作为方法的对象

e、 g


您的问题根源于不理解
静态
,这是正确的。关于这一点有很多参考资料,但这里只需指出,一些
静态的
属于,而一些非静态的则属于特定的实例。也就是说

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.
    }
}
与此相关的是,您的方法
直方图
不是
静态的
,因此您需要一个实例来调用它。你不应该需要一个实例;只需将方法设置为静态:

公共字符串直方图(int randomNum)
更改为
公共静态字符串直方图(int randomNum)

完成后,行
直方图(randomNum)生效。但是,在
System.out.print(直方图+“\n”)
,因为这里定义的
直方图
是一个函数,而不是一个变量。这与
return
语句有关。当某物说
返回x
(对于
x
的任何值),它意味着终止当前方法调用,并将值
x
提供给调用该方法的人

例如,考虑表达式<代码> 2 + 3 < /代码>。如果你说

intx=2+3
,你会期望
x
之后有值
5
。现在考虑一种方法:

public static int plus(int a, int b){
    return a + b;
}
语句:
intx=plus(2,3)。同样,我们希望
x
之后会有值
5
。计算完成后,等待该值(类型为
int
)的人将接收并使用该值,但将使用该类型的单个值代替该值。例如:

intx=plus(加(1,2),加(3,加(4,1));
->
x
的值为11

回到您的示例:您需要为从返回的字符串值分配一个变量<
public static int plus(int a, int b){
    return a + b;
}
public static void main(String[] args){...}
public static void main(String[] Args)
public static void main(String[] Args)
{
     prog310t test = new prog310t();
     test.histogram(1);
}
public static String histogram (int randomNum)
public static void main(String[] Args)
{
     histogram(1);
}
  System.out.print(histogram(randomNum) + "\n");
String test = histogram(randomNum);
System.out.print(test + "\n");
public class Test
{
    public static void main(String[] args)
    {
        getNothingStatic();// this is ok
        getNothing(); // THIS IS NOT OK IT WON'T WORK NEEDS AN INSTANCE
        Test test = new Test();
        test.getNothing(); // this is ok
        getString(); // this is ok but you are not capturing the return value
        String myString = getString(); // now the return string is stored in myString for later use
    }
    public void getNothing()
    {
    }
    public static void getNothingStatic()
    {
    }
    public static String getString()
    {
        return "hello";
    }
}