gpt4 book ai didi

java - 从输入中访问符号之前和之后的索引值

转载 作者:行者123 更新时间:2023-12-01 17:43:21 34 4
gpt4 key购买 nike

我正在尝试获取输入,如果输入中存在@符号,那么它会找到@符号之前和之后的整数的最大值。最大部分我没有问题,但我不知道如何访问和查找@符号之前和之后的值。

import java.util.Scanner;

public class Max_Min {

public static void main(String[] args) {
//gets keyboard
Scanner keyboard = new Scanner(System.in);
//puts input into string
String inputString = keyboard.nextLine();
//splits string between characters
String[] splitInput = inputString.split("");

for (String s : splitInput) {
if(s.equals("@")){
//computes the maximum of the two integers before and after the @
}


}

//close keyboard
keyboard.close();

我确实进行了搜索以找到类似的东西(并且我确信有一些东西),但找不到任何东西。如果有人可以提供帮助那就太好了!

最佳答案

试试这个:

for (int i = 0; i < splitInput.length; i++){
if (splitInput[i].equals("@") && i != 0 && i != splitInput.length -1){
int max = Math.max(Integer.parseInt(splitInput[i - 1]), Integer.parseInt(splitInput[i + 1]));
}
//...
}

关于java - 从输入中访问符号之前和之后的索引值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58295209/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com