简体   繁体   English

当我调用方法 f() 时,控制台中什么也没有出现。 为什么?

[英]When i call the method f() nothing appears in the console. Why?

package Evaluare1;

import java.util.*;

public class e15 {

    public static void main(String[]args){

        Scanner s = new Scanner(System.in);

        int a = s.nextInt();

        f(a);
        //i don't have errors when i run it
        }

    //this method won't return the value when i call it

    public static int f(int x){
        if(x%2 <= 0){
            return x/2;
        }else   
            return x * 3 + 1;
    }
}

How can I fix this?我怎样才能解决这个问题?

If what you want to do is print the result of f() than you cannot simply invoke the function as all that will do is return the result, which you are ignoring.如果您想要做的是打印f()的结果,那么您不能简单地调用该函数,因为所有要做的就是返回您忽略的结果。 Instead try something like this:而是尝试这样的事情:

import java.util.*;
public class e15 {

    public static void main(String[]args){

        Scanner s = new Scanner(System.in);

        int a = s.nextInt();

        System.out.println(f(a));
        //i don't have errors when i run it
    }

    //this method won't return the value when i call it

    public static int f(int x){
        if(x%2 <= 0){
            return x/2;
        }
        else
            return x * 3 + 1;
    }
}

Note the System.out.println(f(a));注意System.out.println(f(a)); . . This will print your result to standard out, typically your shell.这会将您的结果打印到标准输出,通常是您的外壳。

添加 System.out.println() 以在控制台上打印。

If you are trying to learn java try to use different methods present in the PrintStream.class如果您正在尝试学习 Java,请尝试使用 PrintStream.class 中存在的不同方法

For example,例如,

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

        Scanner s = new Scanner(System.in);

        int a = s.nextInt();

        System.out.print(f(a));
        //i don't have errors when i run it
        }

    //this method won't return the value when i call it

    public static int f(int x){
        if(x%2 <= 0){
            return x/2;
        }else   
            return x * 3 + 1;
    }}

In this I have used the print function present in the PrintStream class, it prints the particular integer (overloaded method), you could also use printf, where you want to insert the value at the exact position of the string without concatenation, use %s for string %d for Numbers like that.在此,我使用了 PrintStream 类中存在的打印函数,它打印特定的整数(重载方法),您也可以使用 printf,您想在字符串的确切位置插入值而不连接,使用 %s对于字符串 %d 这样的数字。

The advantages of println is that it prints in the new line. println 的优点在于它在新行中打印。

Play around it would be fun.玩它会很有趣。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在我调用repaint()时什么都没有画? - Why is nothing painted when I call repaint()? 当什么都没有被覆盖时,为什么在构造函数中会收到有关可覆盖方法调用的警告? - Why am I getting warnings for overridable method call in the constructor, when nothing is being overriden? 当我调用repaint()方法时,屏幕上没有任何显示 - Nothing shows up to the screen when I call the repaint() method 我在我的谷歌浏览器开发者工具控制台中看不到 console.log 消息。 任何想法为什么? - I cannot see the console.log messages in my google chrome developer tool console. Any ideas why? 当我尝试在没有主要方法的情况下在 class A 中调用 class B 的方法时出现错误 - Error appears when i try to call a method of class B in class A without main method 为什么数据没有被添加到 map 而相同的 function 可以用于将数据集打印到控制台。 我在这里做错了什么? - Why is the data not getting added to the map while the same function can by used to print dataset to console. What am I doing wrong here? 当我要在Android Studio中构建android项目时,它显示在gradle控制台中构建成功。 但它报告错误 - When I am going to build android project in Android Studio, it shows build success in gradle console. But it reports error 我想在游戏中添加一个计数器,然后将其打印到控制台中。 - I want to add a counter into my game where it prints into the console. 当元素出现 Selenium+Java 时,如何在每次测试中每次调用方法? - How can I call method every time in every test when element appears Selenium+Java? 当该方法单独运行时,我的代码会尝试工作,但是当我在测试程序时调用该方法时…出现捕获错误 - try of my code works when the method is run on its own, but when I call the method when testing my program… catch errors appears
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM