新Java学生的基本问题 [英] Basic Question for new Java Student

查看:81
本文介绍了新Java学生的基本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我没有其他编程经验。我正在努力完成一个家庭作业问题而且我到目前为止还没有。我非常确定这个问题对于这个社区来说非常简单,但我想请求lemans条款帮助。

I am very new to Java and I have no other programming experience. I am struggling with a homework question and I have only gotten so far. I am very sure this question is very simple to this community but I would like to request lemans terms help regarding it.

问题:
编写一个名为reverse的方法,将int作为参数并返回int。该方法反转给定数字的数字并返回它。如果给定5433,则该方法返回3345.

Question: Write a method called reverse, that takes an int as argument and returns an int. The method reverses the digits of the given number and returns it. If given 5433, the method returns 3345.

使用任意值调用main中的方法并将结果打印到屏幕上。

Call the method in a main with an arbitrary value and print the result to the screen.

(这是我到目前为止的代码snippit :(我怎么做反向的话?它可能非常简单,但显然我需要帮助)提前谢谢!)

(This is my code snippit so far: ( how do I do the reverse that is spoke of? its probably very simple but apparently I need help with it) Thanks in advance!)

// Exercise 2.8.3
public class BlackBelt8 {


 public static void main(String[] args) {

  reverse(5433);

 }
 public static int reverse(int x){
  System.out.println(x);
  return x;

 }

}






更新:我处理了所有建议的解决方案。谢谢


Update: I worked through all of the suggested solutions. Thank you

推荐答案

有两种方法可以做到这一点。我不会给你代码,但希望我会把你带到正确的方向。

There are two ways to do this. I won't give you the code, but hopefully I'll put you in the correct direction.


  1. 你可以转整数成一个字符串并反转字符串。然后将其转回整数。如果你想这样做,我建议你看一下Integer.parseInt和String.valueOf。

  1. You can turn the integer into a string and reverse the string. Then turn it back into an integer. If you want to do this, I suggest looking at Integer.parseInt and String.valueOf.

这可能是更好的方法。编程语言中的整数将仅返回答案的商。因此,当您执行5/2时,它将返回2. modulo 将返回除法的其余部分。因此5%2(%是模数运算符)将等于1.

This is probably the better way. Integers in programming languages will return only the quotient of the answer. So when you do 5 / 2, it returns 2. The modulo returns the remainder of the division. So 5 % 2 (% is the modulo operator) would be equal to 1.

观察您可以使用除了10的除法和模数以反转整数。

Observe that you can use the division and modulo of 10 to reverse the integer.

正如其中一条评论中所提到的,你只想从函数中返回结果。从反向函数中删除System.out.println并将其放在main中。所以你会这样称呼:

And as mentioned in one of the comments, you'll want to only return the result from your function. Remove the System.out.println from the reverse function and put it in main. So you would call it like this:

public static void main(String[] args) {
    System.out.println(reverse(123));
}

编辑:这是对评论的回应。

This is in response to the comments.

在Java中,有两种类型的数据。本机类型和对象。本机类型是int,double,float,long,byte和char。这些是不是对象的唯一类型,并且它们不存在toString。它们中的每一个都有一个可以使用的对象。例如,int的对象是Integer。

In Java, there are two types of data. Native types and objects. The native types are int, double, float, long, byte, and char. These are the only types that are not objects and toString doesn't exist for them. Each one of them has an Object that you can use instead though. For example, int's object counterpart is Integer.

其他所有东西都是一个对象,派生自基类对象并与面向对象的编程。 toString方法用于为其所调用的任何对象提供人类可读的字符串表示。

Everything else is an object and is derived from the base class Object and is related to object-oriented programming. The toString method is used to give a human-readable string representation of whatever object it is called on.

任何类都可以拥有自己的toString方法实现,该方法将返回(理想情况下)是一个人类可读的字符串。

Any class can have their own implementation of the toString method which will return (ideally) a human-readable string.

这篇关于新Java学生的基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆