我想检查一个数字中的某些数字,并根据结果打印一条消息。
要检查的数字:7和9 输出:如果是7,打印S和9,打印N(订单很重要)而没有7或9,打印输入编号。
代表:
输入数字:75输出:S(包含7)
输入数字:96输出:N(包含9)
输入数字:79输出:SN
输入数字:97输出:NS
输入数字:67849输出:SN
输入数字:59587输出:NS
输入数字:873579输出:SSN
输入数字:135输出:135
我尝试了以下方法
string output = string.Empty;
int n = 0;
while(number > 0)
{
n = number % 10;
number = number / 10;
if(n == 7)
{
output += "S";
}
if(n == 9)
{
output += "N";
}
}
return string.IsNullOrEmpty(output) ? number.ToString() : output;
但只有当它包含一位数时(例如:适用于17,91)
,这才有效如果它有多个数字(例如:769,957)
,它就无法正常工作如何实现这一目标(订单很重要)。
提前致谢
答案 0 :(得分:2)
我会首先转换为字符串,因此不需要数学计算,它将保留顺序。
string numberStr = number.ToString();
string result = null; // no need to use StringBuilder, because size won't be big
foreach (char c in numberStr)
{
switch (c)
{
case '7':
result +='S';
break;
case '9':
result +='N';
break;
}
}
return string.IsNullOrEmpty(result) ? numberStr : result;
答案 1 :(得分:1)
您可以将您的数字转换为字符串,然后进行迭代,当您找到7时,您将打印S并使用9 N
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.example.root.dravis"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
//adding firebase dependencies manually
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
compile 'com.google.firebase:firebase-database:9.8.00'
testCompile 'junit:junit:4.12'
compile 'com.firebase:firebase-client-android:2.5.2'
}
apply plugin: 'com.google.gms.google-services'
答案 2 :(得分:1)
更简单的方法是先将数字转换为字符串。
var numberStr = number.ToString();
var e = numberStr.Select(x => x == '7' ? "S" : x == '9' ? "N" : "");
var output = string.Join("", e);
return string.IsNullOrEmpty(output) ? numberStr : output;
答案 3 :(得分:1)
using System;
using System.Linq;
namespace Demo
{
class Program
{
static void Main()
{
Console.WriteLine(demo(75));
Console.WriteLine(demo(96));
Console.WriteLine(demo(79));
Console.WriteLine(demo(97));
Console.WriteLine(demo(67849));
Console.WriteLine(demo(59587));
Console.WriteLine(demo(873579));
Console.WriteLine(demo(135));
}
static string demo(int number)
{
string result = new string(
number.ToString().Where(digit => digit == '7' || digit == '9')
.Select(digit => (digit == '7') ? 'S' : 'N' )
.ToArray());
return result.Length > 0 ? result : number.ToString();
}
}
}
备选方案#1:
static readonly string[] lookup = {"", "", "", "", "", "", "", "S", "", "N"};
static string demo(int number)
{
string txt = number.ToString();
string result = string.Concat(txt.Select(digit => lookup[digit-'0']));
return result.Length > 0 ? result : txt;
}
备选方案#2:
static string demo(int number)
{
string txt = number.ToString();
string result = string.Concat(txt.Select(digit => " S N"[digit-'0'])).Replace(" ", "");
return result.Length > 0 ? result : txt;
}
备选方案#3(技术上是"单行"但我把它分开了)
static string demo(int number)
{
return new string(
number.ToString()
.Select(digit => "0123456S8N"[digit - '0'])
.GroupBy(digit => digit == 'S' || digit == 'N')
.OrderBy(g => g.Key)
.Last()
.ToArray());
}
答案 4 :(得分:0)
我修改了你的方法。还有一点,你应该使用StringBuilder而不是string来提高性能。如果你愿意的话,你可以长期代替int。
public static string ProcessValidImport(int number)
{
StringBuilder output = new StringBuilder();
int n = number;
while (number > 0)
{
if (number % 10 == 7)
{
output.Append("S");
}
if (number % 10 == 9)
{
output.Append("N");
}
number = number / 10;
}
return (output == null || output.Length == 0 ) ? n.ToString() : Reverse(output.ToString());
}
public static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
我尝试OP解决方案,他也必须尊重字符串。
你可以使用下面的解决方案,如果你想要SEVEN和NINE而不是S和N.
public static string ProcessValidImport(int number)
{
List<string> output = new List<string>();
int n = number;
while (number > 0)
{
if (number % 10 == 7)
{
output.Add("SEVEN");
}
if (number % 10 == 9)
{
output.Add("NINE");
}
number = number / 10;
}
output.Reverse();
return (output == null || output.Count == 0 ) ? n.ToString() : string.Join("", output.ToArray());
}
答案 5 :(得分:0)
您正在从最后读取您的号码,因此您的结果字符串是向后的。 (997
给出SNN
,当它应该给NNS
)
而不是使用它:
output += "S"; //or "N"
使用:
output = "S" + output; // or "N"