Java 查找字符串中给定变量的值
给出一个等式:Java 查找字符串中给定变量的值,java,Java,给出一个等式: 433+4H8= 871 H可以是等式的任意一边。求H的值,并将其替换为给定的方程 输出应为: 433+438=871 我已经尝试了以下代码,但是字符串到int的转换不起作用 import java.util.Scanner; import java.io.*; class hole { public static void main(String[] args) { Scanner reader= new Scanner(Sys
433+4H8= 871
H
可以是等式的任意一边。求H的值,并将其替换为给定的方程
输出应为:
433+438=871
我已经尝试了以下代码,但是字符串到int的转换不起作用
import java.util.Scanner;
import java.io.*;
class hole
{
public static void main(String[] args)
{
Scanner reader= new Scanner(System.in);
int[] nums= new int[3];
String str= reader.nextLine();
String str_1= str.replaceAll("=","+");
String[] split= str_1.split("[+]");
for (int k=0; k<split.length; k++)
{
String ex= split[k];
nums[k]= Integer.parseInt(ex);
}
}
}
import java.util.Scanner;
导入java.io.*;
班级洞
{
公共静态void main(字符串[]args)
{
扫描仪阅读器=新扫描仪(System.in);
int[]nums=新的int[3];
String str=reader.nextLine();
字符串str_1=str.replaceAll(“=”,“+”);
字符串[]split=str_1.split([+]);
对于(int k=0;k为什么不将最终答案减去第一个数字,然后生成第三个数字。拆分字符串是多余的,因为您可以使用简单的数学来解x
871-433 = 438
h=3
public static void main(String[] args)
{
int a = 433;
int b = 871;
int c = b - a;
String s = "4H8";
char x;
int location = 0;
for(int i = 0; i < s.length(); i++)
{
char d = s.charAt(i);
if(d.isDigit()){
}
else{
location = i;
x = d;
}
}
String solve = c.toString();
System.out.println(x + "=" + solve.charAt(location) );
}
871-433=438
h=3
公共静态void main(字符串[]args)
{
INTA=433;
int b=871;
int c=b-a;
字符串s=“4H8”;
字符x;
int位置=0;
对于(int i=0;i
4H8
不是整数,因此parseInt
对其不起作用
看起来你还有一段路要走,所以我不会因为给你代码而破坏你的乐趣,只是一些想法
假设方程总是X+Y=Z
,其中X、Y和Z是一些整数,其中一个可以包含H
您需要有几个案例:
- 如果
H
在X
中,您可以通过从Z
中减去Y
得到X
- 如果
H
在Y
中,您可以通过从Z
中减去X
得到Y
- 如果
H
在Z
中,您可以通过添加X
和Y
来获得Z
但是这仍然不能给出H
。为此,您可以简单地找到H
在我们拥有的字符串中的位置,并从上面计算的数字中提取该位置的数字
因此,在调用parseInt
之前,您应该查找H
,如果找到该字符串,只需存储该字符串即可
parseInt
也不喜欢空格,所以应该以某种方式删除它们
如果方程式的形式不总是X+Y=Z
(即,您也可以有减法、乘法、除法和/或多个项),它会变得有点复杂。尝试以下方法:
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int[] nums = new int[3];
boolean[] isNum = new boolean[3];
String str = reader.nextLine();
// String str_1= str.replaceAll("=","+");
String[] split = str.split("[+=]");
int i = 0;
for (int k = 0; k < split.length; k++) {
try {
String s = split[k];
isNum[k] = true;
nums[i] = Integer.parseInt(s);i++;
} catch (NumberFormatException e) {
// TODO: handle exception
isNum[k] = false;
}
}
int tot ;
if(str.indexOf("H")>str.indexOf("=")){
tot = nums[1] + nums[0];
}else{
tot = nums[1] - nums[0];
}
for (int k = 0; k < split.length; k++) {
if(!isNum[k]){
int indx = split[k].indexOf("H");
String h = Integer.toString(tot).charAt(indx)+"";
h = str.replace("H",h);
System.out.println("New expression :"+h);
}
}
}
publicstaticvoidmain(字符串[]args)
{
扫描仪阅读器=新扫描仪(System.in);
int[]nums=新的int[3];
boolean[]isNum=新的boolean[3];
String str=reader.nextLine();
//字符串str_1=str.replaceAll(“=”,“+”);
字符串[]split=str.split(“[+=]”);
int i=0;
对于(int k=0;kstr.indexOf(“=”){
tot=nums[1]+nums[0];
}否则{
tot=nums[1]-nums[0];
}
对于(int k=0;k
试试下面的程序,这只适用于输入a+b=c格式
下面的程序给出了正确的输出
import java.util.Scanner;
public class Hole {
public static void main(String args[])
{
int hLocation = 0;
int result;
char hValue;
Scanner in=new Scanner(System.in);
String str=in.nextLine().replaceAll(" ", "");
String[] split= str.split("[+=]");
for(int i=0;i<split.length;i++){
if(split[i].contains("H")){
hLocation=i;
}
}
if(hLocation==0){
result=Integer.parseInt(split[2])-Integer.parseInt(split[1]);
}else if(hLocation==1){
result=Integer.parseInt(split[2])-Integer.parseInt(split[0]);
}
else{
result=Integer.parseInt(split[0])+Integer.parseInt(split[1]);
}
hValue= String.valueOf(result).charAt(1);
str=str.replace('H', hValue);
System.out.println(str);
}
}
import java.util.Scanner;
公开课洞{
公共静态void main(字符串参数[])
{
int hLocation=0;
int结果;
字符值;
扫描仪输入=新扫描仪(系统输入);
字符串str=in.nextLine().replaceAll(“,”);
字符串[]split=str.split(“[+=]”);
对于(int i=0;i试试这个(注释是不言自明的):
publicstaticvoidmain(字符串[]args){
扫描仪阅读器=新扫描仪(System.in);
int[]nums=新的int[3];
int-varIndex=0;
int结果=0;
String str=reader.nextLine();
//删除空格并根据符号拆分为3个部分
String[]components=str.replaceAll(“,”).split(“\\+\124;=”);
对于(int i=0;i
您是否收到任何异常?可能它给您带来了有意义的消息…parseInt无法解析java.lang.NumberFormatException.forInputString(未知源)中的表达式请在您的问题中添加完整的异常stacktrace。然后如何解决它……但是如何从字符串中获取X、Y和Z??获取字符串后,dey不会转换为整数。对于加法或减法,您已经使用parseInt
将其转换为整数,只需检查H
并删除空格即可(假设可以有空格)主要的问题是,如果我用parseint转换它,它会给出numberformatexeptioni在我头顶的黑暗中做了这件事,请告诉我是否有
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int[] nums = new int[3];
int varIndex = 0;
int result = 0;
String str = reader.nextLine();
//remove spaces and split into 3 parts based on symbols
String[] components = str.replaceAll(" ", "").split("\\+|=");
for (int i = 0; i < components.length; i++) {
try {
nums[i] = Integer.parseInt(components[i]);
} catch (Exception e) {
varIndex = i; //this component contains "H"
}
}
//Calculate the value of the number which has "H"
switch (varIndex) {
case 0:
result = nums[2] - nums[1]; break;
case 1:
result = nums[2] - nums[0]; break;
case 2:
result = nums[0] + nums[1]; break;
}
nums[varIndex] = result;
//Print the output
System.out.println(nums[0] + " + " + nums[1] + " = " + nums[2]);
System.out.println("H = " + (String.valueOf(result)).charAt(components[varIndex].indexOf('H')));
}