小编the*_*war的帖子

为什么字节的最大值加一,但没有溢出

class Overflowtest {

  public static void main(String[] args) {

    byte maxValue= Byte.MAX_VALUE;
    System.out.println("maxValue of byte is "+maxValue);
    System.out.println("add 1 to maxValue of byte is "+maxValue+(byte)2);

  }
}
Run Code Online (Sandbox Code Playgroud)

为什么我尝试时没有发生溢出maxValue+(byte)2?但如果我这样做(byte)(maxValue+2),溢出就发生了.

java byte overflow

2
推荐指数
1
解决办法
770
查看次数

具有不同行长度的数组

我必须在java中输出Julian日历.我每个月都打印出来,但是我不确定如何确定每个月对应的行长度.例如,2月,4月,6月,9月和11月没有31天.到目前为止这是我的代码:

 import java.text.DateFormatSymbols;

 public class testing {

 public void Day() {
   System.out.println();
   for (int i = 1; i <= 31; i++) {
     System.out.println(String.format("%2s", i));
   }
 }
 public void MonthNames() {
   String[] months = new DateFormatSymbols().getShortMonths();
   for (int i =  0; i < months.length; i++) {
     String month = months[i];
     System.out.printf(month + "   ");
     //System.out.printf("\t");       
    }    
  }

  public void ColRow() {
    int dayNum365 = 1;
    int array [][] = new int[31][12];
    System.out.println();
    for (int col = 0; col < 12; col++) …
Run Code Online (Sandbox Code Playgroud)

java multidimensional-array

2
推荐指数
1
解决办法
7538
查看次数

从字符串中删除所有\n\r\t的最佳方法是什么?

什么是去除所有的最好办法\n,\r,\tStringjava?是否有某种library并且method可以为我做的很好的我,而不必使用string.replaceAll();多次?

java carriage-return

0
推荐指数
1
解决办法
1543
查看次数

Java基础计算器

我需要构建这个程序,找到三角形的缺失面,但我不断收到错误消息.这是我的代码:

import java.util.Scanner;

public class MissingSide  {

  static java.util.Scanner userInput = new Scanner(System.in);

  public static void main(String[] args) {

    System.out.print("What is the first side, other than the hypotenuse?");

    if (userInput.hasNextInt()) {
      int firstsideGiven = userInput.nextInt();
    } else {
      System.out.println("Enter something acceptable");
    }
    System.out.println("What is the hypotenuse?");

    if (userInput.hasNextInt()) {
      int hypotenuseGiven = userInput.nextInt();
    } else {
      System.out.print("Really?");
    }
    System.out.print("Your missing side value is: " + 
    System.out.print((Math.pow(firstsideGiven, 2) - Math.pow(hypotenuseGiven, 2)) + "this");
  }
}
Run Code Online (Sandbox Code Playgroud)

它一直告诉我"hypotenuseGiven"和"firstsideGiven"无法解析为变量.这是供个人使用,而不是学校用品.谢谢.

java

0
推荐指数
1
解决办法
143
查看次数