EDU>> intmin
ans =
-2147483648
EDU>> abs(intmin)
ans =
2147483647
Run Code Online (Sandbox Code Playgroud)
这怎么可能?必须存在某种溢出,或者这些函数的定义以奇怪的方式混合在一起.
我的代码没有给我正确的输出.它从文本文件中读取数字,将它们转换为整数值并对它们执行一些乘法运算.但是方法findProduct()给我的产品是错误的,所以我认为我使用的Integer.parseInt()方法不正确.
我猜是什么让我失望的是它试图告诉我96086x67017 = 2144428166的产品,当我的计算器检查时它大约是643939395462.
/*
*
* Find the greatest product of five consecutive digits in the 1000-digit number.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
this text file is in Project_8.txt
*/
import java.io.*;
// Import the scanner class that is used for reading the text file
import java.util.*;
public class Project_8 {
// Data members================================
// Object used to read the …Run Code Online (Sandbox Code Playgroud) 我想我明白它是传递给方法的对象/数据成员的副本tricky(),因为只有值才是重要的,而不是实际的对象本身.但是打印语句向我保证,arg1并且arg2副本确实在方法中切换.我不明白为什么这不会将信息传回原始对象,从而切换它们; 看作该方法能够成功访问方法中的arg1.x和arg1.y成员.
// This class demonstrates the way Java passes arguments by first copying an existing
// object/data member. This is called passing by value. the copy then points(refers)
// to the real object
// get the point class from abstract window toolkit
import java.awt.*;
public class passByValue {
static void tricky(Point arg1, Point arg2){
arg1.x = 100;
arg1.y = 100;
System.out.println("Arg1: " + arg1.x + arg1.y);
System.out.println("Arg2: " + …Run Code Online (Sandbox Code Playgroud) 我有语法错误,我不知道如何解决它.代码看起来对我来说是正确的,但Eclipse告诉我"构造函数调用必须是构造函数中的第一个语句",方法setName()和setAge()
public class KeywordThis {
private String name;
private int age;
public KeywordThis(){
this.name = "NULL";
this.age = 0;
}
public KeywordThis(String s, int a){
this.name = s;
this.age = a;
}
public KeywordThis(String s){
this.name = s;
}
public KeywordThis(int a){
this.age = a;
}
public int setAge(int a){
this(a);
}
public String setName(String s){
this(s);
}
public static void main(String args[] ){
}
}
Run Code Online (Sandbox Code Playgroud)