我想創建一個程序,顯示任何給定點的x
和y
座標,反映在線性函數ax+b
上。但是,我得到一個運行時錯誤,說它超出了界限。我知道你不能在原始數據類型上調用方法,但我不知道如何獲得它。charAt()對於大於1的整數?
import java.util.*;
public class ReflectivePoint {
public static void main (String[]args){
Scanner lol = new Scanner(System.in);
System.out.println("Please enter the linear function.");
//That will be in the format ax+b
String function = lol.nextLine();
Scanner lol2 = new Scanner(System.in);
System.out.println("Please enter the point.");
//That will be in the format a,b
String point = lol2.nextLine();
int a = point.charAt(1);
int b = point.charAt(3);
int m = function.charAt(1);
int c = function.charAt(4);
int x1 = (2/(m + 1/m)) * (c + b - a/m) - a;
int y1 = (-1/m) * x1 + a/m + b;
System.out.println(x1+", "+y1);
}
}
'String.charAt(0)'表示第一個字符。 – adatapost
有沒有這樣的事情,編譯器錯誤說索引超出了界限。 – WhyNotHugo
您不需要爲嘗試閱讀的每一行創建一個新的掃描程序;使用同一個。 – WhyNotHugo